i am new in django. so i am trying to create a CRUD application using Django but I am getting "local variable 'all_inf' referenced before assignment " this error while trying to build it
here is my view.py
-- coding: utf-8 --
from future import unicode_literals
from django.shortcuts import render from .forms import Create from enroll.models import User
Create your views here.
def add_show(request):
if request.method == 'POST':
crt = Create(request.POST)
if crt.is_valid():
username = crt.cleaned_data['name']
useremail = crt.cleaned_data['email']
user_contact = crt.cleaned_data['contact_number']
save_to_db = User(name=username, email=useremail,
contact_number=user_contact)
save_to_db.save()
crt = Create()
else:
crt = Create()
all_inf = User.object.all()
return render(request, 'index.html', {'form': crt, 'info': all_inf})
and I am also trying to fetch data from database and show all data in a table but its not showing :(
here is my sample code:
div class="col-sm-8">
<h4 class="text-center alrt alert-info">Showing all results</h4>
{% if all_inf %}
<h4>Table Data</h4>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Contact Number</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{% for inf in info %}
<tr>
<td>{{inf.id}}dfdf</th>
<td>{{inf.name}}</td>
<td>{{inf.email}}}</td>
<td>{{inf.contact_number}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<h4 class="btn btn-danger">No Records Found Try again</h4>
{% endif%}
</div>
</div>