/ accounts / signup /'password'处的MultiValueDictKeyError

问题描述 投票:0回答:1

我是django python的新手,在注册过程中收到此奇怪的错误。

[请让我知道我做错了什么。

这是我在views.py中的代码:

if request.method=='POST':
        #SignUp
        if request.POST['password1'] == request.POST['password2']:

            try:
                user = User.objects.get(username = request.POST.get('username1'))
                return render(request, 'accounts/signup.html', {'error':'username is already taken'})

            except User.DoesNotExist:
                user = User.objects.create_user(username=request.POST['username1'], password=request.POST['password'])
                auth.login(request, user)
                return redirect('home')

    else:
        #enter info
        return render(request, 'accounts/signup.html')

如果输入user= request.POST.get('username1'),无论输入什么,我都会收到“用户名已被占用”的消息。

我的html代码:

<form method="POST" action="{% url 'signup' %}" class="form" autocomplete="off">

        {% csrf_token %}
          <div class="form__group">
              <input type="text" placeholder="username" name="username1" class="form__input" id="username1" autocomplete="off"/>
          </div>

          <div class="form__group" autocomplete="off">
              <input type="email" placeholder="email" name="email" class="form__input" autocomplete="off"/>
          </div>

          <div class="form__group">
              <input type="password" placeholder="Password" name="password1" class="form__input" />
          </div>

          <div class="form__group">
              <input type="password" placeholder="Confirm Password" name="password2" class="form__input" />
          </div>

          <input class="btn btn-primary" type="submit" value="signup!"/>
      </form>

错误是:

MultiValueDictKeyError at /accounts/signup/
'password'
Request Method: POST
Request URL:    http://127.0.0.1:8000/accounts/signup/
Django Version: 2.2.9
Exception Type: MultiValueDictKeyError
Exception Value:    
'password'
Exception Location: C:\Users\HP\Desktop\django\zakevenv\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80
Python Executable:  C:\Users\HP\Desktop\django\zakevenv\Scripts\python.exe
Python Version: 3.7.4
Python Path:    
['C:\\Users\\HP\\Desktop\\django\\producthunt-project',
 'C:\\Users\\HP\\Desktop\\django\\zakevenv\\Scripts\\python37.zip',
 'C:\\Users\\HP\\Desktop\\django\\zakevenv\\DLLs',
 'C:\\Users\\HP\\Desktop\\django\\zakevenv\\lib',
 'C:\\Users\\HP\\Desktop\\django\\zakevenv\\Scripts',
 'C:\\Users\\HP\\Anaconda3\\Lib',
 'C:\\Users\\HP\\Anaconda3\\DLLs',
 'C:\\Users\\HP\\Desktop\\django\\zakevenv',
 'C:\\Users\\HP\\Desktop\\django\\zakevenv\\lib\\site-packages']
Server time:    Thu, 23 Jan 2020 15:20:06 +0000

我花了几个小时,但我仍然不知道我在哪里做错了:(。任何帮助将不胜感激。

python django username
1个回答
1
投票
if request.method=='POST':
        #SignUp
        if request.POST['password1'] == request.POST['password2']:

            try:
                user = User.objects.get(username = request.POST.get('username1'))
                return render(request, 'accounts/signup.html', {'error':'username is already taken'})

            except User.DoesNotExist:
                user = User.objects.create_user(username=request.POST['username1'], password=request.POST['password1'])
                auth.login(request, user)
                return redirect('home')

    else:
        #enter info
        return render(request, 'accounts/signup.html')

错误:如果您尝试使用try块,则尝试获取用户名,则您没有通过该用户名执行任何操作,然后在render方法中,您通过分配信息来发送错误消息。因此,由于您将其作为默认消息发送,因此在模板文件上显示错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.