【发布时间】:2021-05-04 04:43:31
【问题描述】:
我尝试使用 'POST' 方法创建一个简单的注册形式,然后在views.py 我尝试了这个:
from django.shortcuts import render,redirect
from django.contrib.auth.models import User,auth
# Create your views here.
def register(request):
if request.POST:
first_name = request.POST["first_name"]
last_name = request.POST["last_name"]
username = request.POST["username"]
email = request.POST["email"]
password = request.POST["password1"]
password2 = request.POST["password2"]
user = User.objects.create_user(
username=username,
password=password,
email=email,
first_name=first_name,
last_name=last_name
)
user.save()
return redirect("/")
else:
return render(request, "register.html")
为了防止页面再次像“..../register/register”一样转到相同的网址,因为我一直在使用同一个文件“register.html”来提交数据和获取页面:
<form action="register" method="post">
{%csrf_token%}
<br>
First_Name : <br> <input type="text" maxlength="30"
placeholder="First Name" name="first_name" required>
<br>
Last_Name : <br> <input type="text" maxlength="30"
placeholder="Last Name" name="last_name" required>
<br>
User_Name : <br> <input type="text" maxlength="30"
placeholder="User Name" name="username" required>
<br>
Email : <br> <input type="email" placeholder="Email"
name="email" required>
<br>
Password :<br> <input type="password" maxlength="30"
placeholder="Password" name="password1" required>
Confirm_Password :<br> <input type="password" maxlength="30"
placeholder="Confirm Password" name="password2" required>
<hr>
<input type="submit">
</form>
这是我的项目文件网址:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('website.urls')),
path('accounts/', include('accounts.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
问题是当我单击提交时...它不执行if request.method == 'POST' 中的代码。它返回同一页面的另一个 URL。就像不存在的“....accounts/register/register”。
知道可能导致问题的原因。谢谢
【问题讨论】:
-
您的问题到底是什么?你没有提到它
-
提及您的确切错误。
标签: django django-models django-views django-forms django-urls