简体   繁体   English

在一个视图中尝试多格式时出现局部变量错误

[英]local variable error when trying multiform in one view

I was searching the internet for a solution for using two forms in one view, and found this Proper way to handle multiple forms on one page in Django . 我在互联网上寻找一种在一个视图中使用两种形式的解决方案,并在Django中找到了一种在一页上处理多种形式的正确方法

But I get local variable 'loginform' referenced before assignment error when trying to post any of the forms. 但是当尝试发布任何表单时,我在赋值错误之前得到了引用的局部变量'loginform'。

Trackback 追溯

Request Method: POST
Request URL: http://127.0.0.1:8000/

Django Version: 1.8.3
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'main')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Users/eisamazrouei/Desktop/django/imhere/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/eisamazrouei/Desktop/django/imhere/imhere/main/views.py" in landing_page
  36.   return render(request,'main/landing_page.html',{'loginform':loginform,'registerform':registerform})

Exception Type: UnboundLocalError at /
Exception Value: local variable 'loginform' referenced before assignment

views.py views.py

def landing_page(request):
if request.method == "POST":
    if 'Login' in request.POST:
        loginform = LoginForm(request.POST)
        if loginform.is_valid():
            username = form.cleaned_data['l_username']
            password = form.cleaned_data['l_password']
            user = authenticate(username=username,password=password)
            login(request,user)
            return redirect(reverse('main_page',args=[request.user.username]))
        registerform = RegisterForm()
    elif 'Register' in request.POST :
        registerform = RegisterForm(request.POST)
        if registerform.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            email = form.cleaned_data['email']
            user = User.objects.create_user(username,email,password)
            login(request,user)
            return redirect(reverse('edit_page',args=[request.user.username]))
        loginform = LoginForm()
else :
    loginform = LoginForm()
    registerform=RegisterForm()
return render(request,'main/landing_page.html',{'loginform':loginform,'registerform':register form})

landing_page.html landing_page.html

<div id=login>
    <form action="" method="POST">
    {% csrf_token %}
    {{loginform.as_p}}
    <input type="submit" value="Login"/>
    </form>
</div>


<div id=register>
    <form action="" method="POST">
    {% csrf_token %}
    {{registerform.as_p}}
    <input type="submit" value="Register"/>
    </form>
</div>

please let me know if the method I'm using is wrong. 请让我知道我使用的方法是否错误。

Thanks 谢谢

You're not sending either Login or Register in your post data, so neither of the conditions will ever be true on post and the variables remain undefined. 您不会在帖子数据中发送“登录”或“注册”信息,因此,发布时都不会满足任何条件,并且变量仍未定义。

You have given your submit buttons values, but not names, so they cannot be included in the post data. 您已为提交按钮指定了值,但未指定名称,因此它们不能包含在帖子数据中。 Do this: 做这个:

<input type="submit" name="Login" value="Login"/>
...
<input type="submit" name="Register" value="Register"/>

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

相关问题 尝试获取表情符号 ID 并进行比较时出现“分配前引用的局部变量”错误 - "local variable referenced before assignment" error when trying to get emoji ID and compare it 尝试乘以变量时出错? - Error when trying to multiply a variable? 为什么在尝试用python编程堆栈时将局部变量声明为全局变量时出现局部变量错误? - why am I getting an error of local variable when I have declared it as a global variable, whilst trying to program a stack in python? 当在本地var上操作时,获取到本地var的会话变量会发生变化 - Session variable fetched into a local var changes when operating on the local one 导入信号时未绑定的局部变量错误? - Unbound local variable error when importing signal? 调用类时局部变量引用错误 - Local variable referenced error when calling class 尝试 plot 时在赋值之前引用的局部变量 - local variable referenced before assignment when trying to plot 尝试插入变量时出错 - Getting error when trying to interpolate variable 分配与全局变量同名的局部变量时出错 - Error when assigning local variable with same name as a global variable 尝试在本地运行celery时收到错误消息 - when trying to run celery local I get an error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM