填写表单后单击“提交”时,会发生此错误。我打算按照本教程进行操作,但使用引导模板登录页面https://dev.to/coderasha/create-advanced-user-sign-up-view-in-django-step-by-step-k9m
views.py
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from .forms import SignUpForm
from django.shortcuts import render, redirect
def home(request):
return render(request, 'home.html')
def signup(request):
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect('home')
else:
form = SignUpForm()
return render(request, 'signup.html', {'form': form})
app1 / urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name="home"),
path('signup/', views.signup, name='signup'),
]
signup.html
<form role="form" action="signup" method="POST">
{% csrf_token %}
forms.html
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class SignUpForm(UserCreationForm):
username = forms.CharField(max_length=30)
email = forms.EmailField(max_length=200)
class Meta:
model = User
fields = ('username','first_name','last_name','email', 'password1', 'password2', )