为什么收到此错误“分配前已引用本地变量'文本'”

时间:2019-12-05 23:03:15

标签: python django

我正在尝试使用python和Django创建一个共享函数,当我运行“ share”时,它会给我一个错误。这是我的代码:

views.py

    from django.shortcuts import render
from basicapp.forms import UserForm, UserProfileInfoForm, PostForm
from django.contrib.auth.models import User
from basicapp.models import UserProfileInfo


from django.urls import reverse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth import login,logout,authenticate
@login_required
def user_post(request):
    form = PostForm(request.POST)
    if form.is_valid():
        text = form.cleaned_data['post']
        form = PostForm()
        return HttpResponseRedirect(reverse('index'))
    else:
        HttpResponse("Not Valid try dat boi agian")

    render(request, 'basicapp/userpost.html', 
                    {'form':form,
                     'text':text})

forms.py

class PostForm(forms.Form):
    post = forms.CharField(max_length=256)

This is the error:

1 个答案:

答案 0 :(得分:0)

您不会在执行 proxy_ssl_certificate /etc/nginx/ssl/backend.crt; proxy_ssl_certificate_key /etc/nginx/ssl/backend.key; 的过程中返回HttpResponse,而是继续执行下一行要定义else变量的下一行代码,但是由于text块永远不会运行,范围内没有is_valid

一旦在text测试的returnif分支中都有else,您将永远也不会到达最后一行。

form.is_valid()