简体   繁体   English

请我是 python 的新手,我正在尝试使用 Django 做一个食谱应用程序。 我遇到了一个错误,即“UnboundLocalError:局部变量形式

[英]Please I'm new to python and I'm trying to do a recipe app using Django. I encountered an error which is "UnboundLocalError: local variable form

this is the picture of my views.py这是我的views.py的图片

When I ran the code I saw an this error in the browser UnboundLocalError: local variable 'form' referenced before assignment当我运行代码时,我在浏览器中看到了这个错误 UnboundLocalError: local variable 'form' referenced before assignment

you only create form if your request is POST.仅当您的请求是 POST 时才创建表单。 so if you try to call your view with get method form variable is not created.因此,如果您尝试使用 get 方法调用视图,则不会创建表单变量。


this is your code I'm gonna walk you through it.这是你的代码,我将引导你完成它。

def register(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request,f"{username}, your account has been created!")
            return redirect('recipes-home')
        else:
            form = UserCreationForm()
    return render(request,'users/Register.html',{'form',form})

if a user post something to your register view this happens: they go in this condition:如果用户将某些内容发布到您的注册视图,则会发生这种情况:他们在这种情况下是 go:

form = UserCreationForm(request.POST)
if form.is_valid():
    username = form.cleaned_data.get('username')
    messages.success(request,f"{username}, your account has been created!")
    return redirect('recipes-home')
else:
    form = UserCreationForm()

if data is valid they get redirected.如果数据有效,它们会被重定向。 else an empty form is created and then this line:否则创建一个空表单,然后是这一行:

return render(request,'users/Register.html',{'form',form})

works because it's out of if condition and an empty form exists.有效,因为它超出了 if 条件并且存在空表单。 but if a user tries to request the view with get method this code executes because condition checks for "POST":但是如果用户尝试使用 get 方法请求视图,则会执行此代码,因为条件检查“POST”:

def register(request):
    return render(request,'users/Register.html',{'form',form})

so now you can see that your code can't understand where did the form came from.所以现在您可以看到您的代码无法理解form的来源。

you can use this to make your code work.您可以使用它来使您的代码正常工作。

def register(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request,f"{username}, your account has been created!")
            return redirect('recipes-home')
        else:
            form = UserCreationForm()
    if request.method == "GET":
        form = UserCreationForm()
    return render(request,'users/Register.html',{'form',form})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试在 django 中制作 model。 但错误。 为什么? - I'm trying to make model in django. but error. why? 我正在尝试使用 django 登录。 但总是失败。 为什么? - I'm trying to login with django. but always fail. why? 我正在尝试将 celery 应用于 django 中基于 Class 的视图(apis)。 我怎样才能做到这一点? - I'm trying to apply celery to Class based view(apis) in django. How can I do this? 我收到此错误 UnboundLocalError: local variable 'current' referenced before assignment - I'm getting this error UnboundLocalError: local variable 'current' referenced before assignment 我正在使用 django 创建一个日历应用程序。 日历必须是垂直的 - I`m creating a calendar-app using django. Calendar must be vertical 我正在尝试在 Python Django 中访问 HTML 表单数据, - I'm Trying to access HTML form data in Python Django, 我正在执行此代码,并且收到 UnboundLocalError: local variable 'points' referenced before assignment - I'm doing this code and I am receiving an UnboundLocalError: local variable 'points' referenced before assignment 我如何知道我使用的是哪个 Python 解释器? - How do I tell which Python interpreter I'm using? 设置SocketIO和Flask我收到此错误:“ UnboundLocalError:分配前引用了本地变量'ssl_socket'” - Setting up SocketIO and Flask I'm getting this error: “UnboundLocalError: local variable 'ssl_socket' referenced before assignment” 应用程序(Python Django,PostgreSql)已成功部署在Heroku上,但是尝试打开时出现错误 - App (Python Django, PostgreSql) is deployed successfully on Heroku, but I'm getting error when trying to open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM