My code is of an enrolment list for a student. It shows subjects a student can enroll and ones the student already has enrolled and can tick as passed, delete or unpass the passed ones. The problem is when I want to add/enroll a new subject I get the following error. If someone can take a look and help, much appreciated.
TRACEBACK
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/enrolment/
Django Version: 3.1.5
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'App',
'crispy_forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:UsersDDocumentsdjangoprojectenvlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
response = get_response(request)
File "C:UsersDDocumentsdjangoprojectenvlibsite-packagesdjangocorehandlersase.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersDDocumentsdjangoprojectprojectAppdecorations.py", line 15, in wrap
return function(request, *args, **kwargs)
File "C:UsersDDocumentsdjangoprojectprojectAppviews.py", line 73, in enrolment_view
enroll(request.POST.get('enroll'),student)
Exception Type: UnboundLocalError at /enrolment/
Exception Value: local variable 'enroll' referenced before assignment
MY VIEW
def enroll(subject_id, student):
if not Enrolment.objects.filter(student_id_id=student.id, predmet_id_id=subject_id):
Enrolment.objects.create(student_id_id=student.id, predmet_id_id=subject_id, status='enrolled')
def delete(subject_id, student):
Enrolment.objects.filter(predmet_id_id=subject_id, student_id_id=student.id).delete()
def passed(subject_id, student):
Enrolment.objects.filter(predmet_id_id=subject_id, student_id_id=student.id).update(status='passed')
def not_passed(subject_id, student):
Enrolment.objects.filter(predmet_id_id=subject_id, student_id_id=student.id).delete()
@student_required
def enrolment_view(request):
if (request.user.is_authenticated):
username = request.user.get_username()
student = MyUser.objects.get(username=username)
if(request.method == 'POST'):
if request.POST.get('enroll'):
enroll(request.POST.get('enroll'),student)
elif request.POST.get('delete'):
delete(request.POST.get('delete'),student)
elif request.POST.get('passed'):
passed(request.POST.get('passed'),student)
elif request.POST.get('not_passed'):
not_passed(request.POST.get('not_passed'),student)
enroll = Enrolment.objects.filter(student_id_id=student.id).order_by('predmet_id')
subjects = Subject.objects.exclude(id__in=enroll.values('predmet_id'))
subj_all = Subject.objects.all()
if student.status == 'REDOVNI':
br_sem = 6
else:
br_sem = 8
context = {
'subjects':subjects,
'enroll':enroll,
'student':student,
'semestar':range(1, br_sem + 1),
'all':subj_all
}
return render(request, 'enrolment.html', context)
else:
return redirect ('login')
MY TEMPLATE
{% extends 'base.html' %}
{% block content %}
<h1>Upisni list</h1>
<div class='row'>
<div class = 'border col-md-6 flrx-column'>
<h1>Predmeti</h1>
{% for subj in subjects %}
<form method = 'POST'>{% csrf_token %}
<button type='submit' name='enroll' value="{{subj.id}}" class="btn btn-outline-dark">+</button>
{{subj.kod}} {{subj.ime}}
</form>
{% endfor %}
</div>
<div class = 'border col-md-5 flrx-column'>
<h1>Student {{student.username}} predmeti</h1>
{% for sem in semestar %}
<div class = 'border'>
<h3>Semestar {{sem}}</h3>
{% for subject in all %}
{% for enrl in enroll %}
{% if subject.id == enrl.predmet_id_id %}
{% if student.status == "REDOVNI" and subject.sem_redovni == sem %}
<form method = 'POST' class="d-flex justify-content-between align-items-center">{% csrf_token %}
{{subject.kod}}{{subject.ime}}
<div class="d-flex justify-content-end">
{% if enrl.status == 'enrolled' %}
<button type="submit" name="delete" value="{{subject.id}}"class="btn btn-outline-dark">x</button>
<button type="submit" name="passed" value="{{subject.id}}"class="btn btn-outline-dark"> ✔</button>
{% elif enrl.status == 'passed' %}
<span> ✔</span>
<button type="submit" name="not_passed" value="{{subject.id}}"class="btn btn-outline-dark"> Unpass</button>
{% endif %}
</div>
</form>
{% elif student.status == "IZVANREDNI" and subject.sem_izvanredni == sem %}
<form method = 'POST' class="d-flex justify-content-between align-items-center">{% csrf_token %}
{{subject.kod}}{{subject.ime}}
<div class="d-flex justify-content-end">
{% if enrl.status == 'enrolled' %}
<button type="submit" name="delete" value="{{subject.id}}"class="btn btn-outline-dark">x</button>
<button type="submit" name="passed" value="{{subject.id}}"class="btn btn-outline-dark"> ✔</button>
{% elif enrl.status == 'passed' %}
<span> ✔</span>
<button type="submit" name="not_passed" value="{{subject.id}}"class="btn btn-outline-dark"> Unpass</button>
{% endif %}
</div>
</form>
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endfor %}
</div>
</div>
{% endblock %}
question from:
https://stackoverflow.com/questions/65886130/django-unboundlocalerror-local-variable-enroll-referenced-before-assignment