local variable 'data' referenced before assignment
unboundlocalerror /邮件/ 局部变量的引用之前的配置数据 请求的方法: 邮件 请求的URL。 http:// 127.0.0.1:8000 /邮件/ Django的版本: 1.11.3 例外的类型: unboundlocalerror 例外的价值: 局部变量的引用之前的配置数据 位置:例外 D: views.py formapp网站的指标,线56
1 2 3 4 5 6 7 8 9 10 11 12 | def index(request): if request.method == 'POST': form = Postform(request.POST,request.FILES) if form.is_valid(): f=form.save(commit=False) f.user=request.user f.save() return HttpResponseRedirect('/post/') else: form = Postform() data=Post.objects.order_by('-date_created') return render(request,'home2.html',{'form':form,'data':data}) |
1 2 3 4 5 6 7 | class Postform(forms.ModelForm): pic=models.FileField() vid=models.FileField() class Meta: model=Post #fields=['title','content','pic'] fields='__all__' |
1 2 3 4 5 6 7 8 | <form action=' ' method = 'POST' enctype="multipart/form-data"> {% csrf_token %} Title: {{form.title}} Content: {{form.content}} Pic: {{form.pic}} <button type= 'submit' class='btn btn-primary btn-md'>Save</button> </form> |
如果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def index(request): if request.method == 'POST': form = Postform(request.POST,request.FILES) if form.is_valid(): f=form.save(commit=False) f.user=request.user f.save() return HttpResponseRedirect('/post/') else: form = Postform() data=Post.objects.order_by('-date_created') # take this out of the `else` block. return render(request,'home2.html',{'form':form,'data':data}) |