Django: MultiValueDictKeyError



我创建了这个注册表单,它运行得非常好。在同一个模板上,我创建了一个登录表单。注册表单有三个参数:电子邮件、用户名和密码,而登录表单只有用户名和密码输入。当我尝试注册时,一切都很好,但当我尝试登录时,它会给我MultiValueDictKeyError

这是我的看法.py

from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth import login, logout, authenticate
from django.db import IntegrityError
# Create your views here.
def usersignup(request):
    if request.method == 'GET':
        return render(request, 'social_media/user.html')
    else:
        try:
            user = User.objects.create_user(username=request.POST.get['username'], password=request.POST.get['password'], email=request.POST['email'])
            user.save()
            login(request, user)
            return render(request, 'social_media/base.html')
        except IntegrityError:
            return render(request, 'social_media/user.html', {'error':'User already exists'})
def loginuser(request):
    if request.method == 'GET':
        return render(request, 'social_media/user.html')
    else:
        user = authenticate(request, username=request.POST.get['username'], password=request.POST.get['password'])
        if user is none:
            return render(request, 'social_media/user.html', {'errorsignin':'Invalid User man'})
        else:
            login(request, user)
            return render(request, 'social_media/base.html')

这是我的HTML表单:

{% load static %}
<form action="" method="POST">
    {% csrf_token %}
    <input type="text" name="username" placeholder="Username">
    <input type="email" name="email" placeholder="Email">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Sign In</button>
</form>
                
<form action="" method="POST">
    {% csrf_token %}
    <input type="text" name="username" placeholder="Username">
    <input type="email" name="email" placeholder="Email">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Sign Up</button>
</form>
   

有没有什么方法可以让我只使用用户名和密码来注册用户,而不使用电子邮件,或者我必须添加一个电子邮件输入字段来注册用户!

问候

Vansh

替换

request.POST['']

带有

request.POST.get('')

无处不在的

示例

request.POST['username']

request.POST.get('username')

get是一种在视图中将get[]替换为get((的方法。py
在html 中纠正表单操作

相关内容

  • 没有找到相关文章

最新更新