local variable referenced before assignment django error
Solution 1
in else part of views.py you mentioned researcher variable in render method that is producing this error.
so please add
researcher = None
before if statement
and also add
study = None
that will also create same error
forms.py
from django import forms
from .models import AboutHelp
class AboutHelpForm(forms.ModelForm):
class Meta:
model = AboutHelp
fields = '__all__'
views.py
def about_experiment(request,ex_link_name):
researcher = None
study = None
form = AboutHelpForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form.save()
return render(request, 'about_experiment.html', {'researcher': researcher, 'study': study})
Solution 2
researcher
and study
are not assignment if request method is not POST and form is not valid. You should define this variable before if statement:
def about_experiment(request,ex_link_name):
researcher = ''
study = ''
if request.method == 'POST':
...
unknown
Updated on June 27, 2022Comments
-
unknown almost 2 years
This is my view.py and when i have a form which when i submit with the required fields it gives an appropriate output but when i don't input anything in the form and click submit i get an error saying "local variable 'researcher' referenced before assignment".
Also i want to know how do i keep my form data saved on the destination page
def about_experiment(request,ex_link_name): if request.method == 'POST': form = AboutHelp(request.POST) if form.is_valid(): researcher = form.cleaned_data['researcher'] study = form.cleaned_data['study'] else: form = AboutHelp() return render(request, 'about_experiment.html', {'researcher': researcher, 'study': study})
my form on the source page is
<form action="{% url 'lazer.views.about_experiment' exp.link_name %}" method="POST" name="form"> {% csrf_token %} <label>Researcher Name(s):<input type="text" name="researcher"> <lable>Study Summary<textarea rows="10" cols="50" placeholder="here you go" maxlength="500" class="form-control" name="study"></textarea> <br> <input type = "submit" value="Submit" class="btn btn-primary" /> </form>
My destination page where the form outputs are present
<h4> Name : {{ researcher }} </h4><br> <h4> Summary : {{ study }} </h4>
-
unknown almost 7 yearsThat worked well. Thanks. Also how do I save the Form data on the destination page permanently?
-
unknown almost 7 yearsBut when i am just filling the researcher text box in the form and keeping the study summary box empty and when i submit the form, its giving me none and none in both. How do i handle that exception.
-
Neeraj Kumar almost 7 yearsAccording to your code you did not save form data into table. So firstly use form.save() method at last of form.is_valid() method.
-
unknown almost 7 yearsI had already tried that but it gives me an error 'AboutHelp' object has no attribute 'save'
-
Neeraj Kumar almost 7 yearswhat is AboutHelp type. it should be type of form class not model class.
-
unknown almost 7 yearsthis is my forms.py class AboutHelp(forms.Form): researcher = forms.CharField(max_length=100) study = forms.CharField(max_length=500)
-
Neeraj Kumar almost 7 yearsyou are using simple Form not Modelform, So if you will use Modelform then that save() method will work and according to your simple form you have to pass both var to model class for save like : AboutHelp.objects.create(researcher=researcher, study=study) and read full documentation of django forms
-
unknown almost 7 yearscould you help me write the code for modelform if possible
-
unknown almost 7 yearsafter i submit the form its giving an error no such table
-
Neeraj Kumar almost 7 yearsBecause you did not migrate django model So run this command: python manage.py migrate . you should follow django tutorials from starting.
-
unknown almost 7 yearsStill it is not saving on the destination page the values for future reference. If i redirect again to that page its giving fields as None only.
-
neverwalkaloner almost 7 years@unknown try to use
request.session
stackoverflow.com/questions/32787838/…