gpt4 book ai didi

Django 3.0 值错误

转载 作者:行者123 更新时间:2023-12-04 10:53:28 25 4
gpt4 key购买 nike

大家好,django 3.0 有问题。我正在尝试创建一个用户注册页面,但我不断收到错误消息。这些是我的代码

用户/views.py

from .form import UserRegisterForm
from django.shortcuts import render, redirect


# Create your views here.


def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'register.html', {'form': form})

form.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class UserRegisterForm(UserCreationForm):
email = forms.EmailField()

class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']

注册.html
{% extends 'website/base.html' %}


{% block body %}
{% include 'website/nav_bar.html' %}

<div>
<form method="post">
{% csrf_token %}
<fieldset>
<legend>join now</legend>
{{ form.as_p }}
</fieldset>
<button type="submit">submit</button>
</form>
</div>
{% endblock body %}

ERROR MESSAGE

最佳答案

这里

def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'register.html', {'form': form})

您忘记了正常情况 GET要求 :)

类似的东西
def register(request):
form = UserRegisterForm()

if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
return render(request, 'register.html', {'form': form})

更多详情请访问文档网站 https://docs.djangoproject.com/en/3.0/topics/forms/#the-view

关于Django 3.0 值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59350772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com