Issue
I am using request.session to pass value from one page to another in django but apparantly when you trigger the latter function,it does not update the variable.
views.py
first function`
def form_CDC(request):
global FIC
if request.method == 'GET':
cdc_name = request.GET.get('data')
if request.method == 'POST':
form = classForm(request.POST)
if form.is_valid():
FIC=form.cleaned_data["FIC"]
request.session['Tuts']=form.cleaned_data["Tutorials"]
request.session['Lec']=form.cleaned_data["Lectures"]
request.session['Lab']=form.cleaned_data["Labs"]
return HttpResponseRedirect("form_Faculty")
else:
form = classForm()
return HttpResponseRedirect('form_CDC')
else:
form = classForm()
return render(request, "homepage/form_CDC.html", context={'form': form,"cdc_name":cdc_name})
second function
def form_faculty(request):
submitted=False
#these lines of code are not triggered when page is loaded
Tutorial_number=request.session["Tuts"]
Lab_number=request.session["Lab"]
Lecture_Number=request.session["Lec"]
Lab_Faculty=[]
Tut_Faculty=[]
Lec_Faculty=[]
Lectureformset=formset_factory(facultyform1,extra=Lecture_Number)
Tutformset=formset_factory(facultyform2,extra=Tutorial_number)
Labformset=formset_factory(facultyform3,extra=Lab_number)
if request.method=="POST":
form_lec=Lectureformset(request.POST or None)
form_tut=Tutformset(request.POST or None)
form_lab=Labformset(request.POST or None)
if all([form_lec.is_valid(),form_tut.is_valid(),form_lab.is_valid()]):
for form1 in form_lec.forms:
Lec_Faculty.append(form1.cleaned_data['Faculty'])
for form2 in form_tut.forms:
Tut_Faculty.append(form2.cleaned_data['Faculty'])
for form3 in form_lab.forms:
Lab_Faculty.append(form3.cleaned_data['Faculty'])
create_file(FIC_name=FIC,cdc=cdc_name,Lecture=Lecture_Number,Tutorial=Tutorial_number,Lab=Lab_number,Faculty_Lab=Lab_Faculty,Faculty_Lec=Lec_Faculty,Faculty_Tut=Tut_Faculty)
return HttpResponseRedirect('choose_new_table?submitted=True')
return render(request, 'homepage/facultyForm.html', {'Lectureformset': Lectureformset,'Tutformset':Tutformset,'Labformset':Labformset,"submitted":submitted})
urls.py
urlpatterns = [
path('form_CDC',views.form_CDC,name='form_CDC'),
path('form_Faculty',views.form_faculty,name='form_Faculty'),
]
`
I feel its a dumb intendetion mistake since it was working fine before but now trying every possible outcome,nothing I can think of is remaining.Hope someone can help me out.
Solution
Request with method "post" can not be redirected as post request.
You should send a new request or call the function directly:
def form_CDC(request):
...
if request.method == 'POST':
...
return form_Faculty(request)
...
By the way, use GCBV Django.
Answered By - Maxim Danilov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.