0
votes

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>
1

1 Answers

0
votes

It's because the there is no all_inf in the if clause. However, the return statement is executed in all cases.

Soln - Put some default value of all_inf before the outer if else block.

Either change the return statement

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()
    return render(request, 'index.html', {'form': crt}) #this is not having the ```all_inf```
else:
    crt = Create()
    all_inf = User.object.all()
    return render(request, 'index.html', {'form': crt, 'info': all_inf})

OR keep the code same and define the all_inf as None or empty list([]) as per your template code

all_inf = None
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})