繁体   English   中英

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

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

我在互联网上寻找一种在一个视图中使用两种形式的解决方案,并在Django中找到了一种在一页上处理多种形式的正确方法

但是当尝试发布任何表单时,我在赋值错误之前得到了引用的局部变量'loginform'。

追溯

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

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

<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>

请让我知道我使用的方法是否错误。

谢谢

您不会在帖子数据中发送“登录”或“注册”信息,因此,发布时都不会满足任何条件,并且变量仍未定义。

您已为提交按钮指定了值,但未指定名称,因此它们不能包含在帖子数据中。 做这个:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM