html密码字段在django1.9中返回空

时间:2016-02-25 12:05:26

标签: python html django html5 django-views

我为用户注册和登录页面开发了名为“login”的django应用程序名称

  • 我创建了一个html页面register.html它包含带有用户名和密码字段的表单,表单方法“post”
  • 当我提交表单时,即使我输入该字段,密码字段也总是空着

login.views.py

def submitreport(request):
errors=[]
if request.method =='POST':
    if not request.POST.get('m_id', ''):
        errors.append('Enter a m_id.')
    if request.POST.get('email') and '@' not in request.POST['email']:
        errors.append('Enter a valid e-mail address.')
    if not request.POST.get('passcode', ''):  #this triggered even when i typed
        errors.append('Enter password')
    if not errors:
        m_id= request.POST.get('id', '')
        Email = request.POST.get('email', '')
        password =request.POST.get('passcode','')
        print(password)
        user = User.objects.create_user(m_id,Email,password)
        messages.success(request, "Successfully registered")
        return HttpResponseRedirect('/')
return render(request, 'login/register.html',{'errors': errors})

register.html

<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>  
{% if errors %}
    <ul>
        {% for error in errors %}
        <li>{{ error }}</li>
        {% endfor %}
    </ul>
 {% endif %}
 <form action="{% url 'login:submitreport' %}" method="post">
    <p>Machine id : <input type="text" name="m_id" value="{{ m_id }}"></p>
    <p>e-mail (optional): <input type="text" name="email" value="{{ email }}"></p>
    <p>password: <input type="password" name="psw" value="{{ passcode }}"></p>
    <input type="submit" value="Submit">
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你应该在你看来:

password =request.POST.get('psw','')

password =request.POST.get('passcode','')