django 函数在调用时未正确触发

bwntbbo3  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(114)

我使用request.session在django中将值从一个页面传递到另一个页面,但显然当你触发后一个函数时,它不会更新变量。
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'),
]


`
我觉得这是一个愚蠢的意图错误,因为它以前工作得很好,但现在尝试每一个可能的结果,没有什么我能想到的是剩余的。希望有人能帮助我。

ttygqcqt

ttygqcqt1#

方法为“post”的请求不能作为post请求重定向。
你应该发送一个新的请求或者直接调用函数:

def form_CDC(request):
    ...
    
    if request.method == 'POST':

        ...

        return form_Faculty(request)
    ...

字符串
使用GCBV Django。

相关问题