我想直接在索引
.html
页面(主页)查看注册表。在index.html中,我想显示register.html
。
注册表单在registration/register.html 中创建。我在index.html中使用以下方式调用它:
{% block content %}
{% include 'registration/register.html' %}
{%endblock%}
在
urlpatterns
的App1/views.py
中,我使用:
path("register_request", views.register_request, name='register_request')
问题是在index.html上我看到了表单按钮,但看不到它的文本框(用户名、电子邮件、密码)。
我不明白我错在哪里。你能告诉我我做错了什么吗?你能告诉我代码如何修复它吗?
这是
App1
:
App1
.migrations
.static
.templates
..index.html
..registration
...register.html
index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>University</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
<link href="/static/css/style.css" rel="stylesheet">
</head>
<body>
.......
.......
{% block content %}
{% include 'registration/register.html' %}
{%endblock%}
</body>
</html>
App1/templates/registration/register.html
<!--Register-->
<div class="container py-5">
<h1>Register</h1>
<form method="POST">
{% csrf_token %}
{{ register_form.as_p }}
<button class="btn btn-primary" type="submit">Register</button>
</form>
<p class="text-center">If you already have an account, <a href="/login">login</a> instead.</p>
</div>
App1/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
# Create your forms here.
class NewUserForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(NewUserForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
App1/urls.py
from django.urls import path
from . import views
urlpatterns=[
path('', views.index, name='index'),
path("register_request", views.register_request, name='register_request'),
]
App1/views.py
from .forms import NewUserForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
def index(request):
"""View function for home page of site."""
return render(request, 'index.html')
def register_request(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registration successful." )
return redirect("index.html")
messages.error(request, "Unsuccessful registration. Invalid information.")
form = NewUserForm()
return render (request=request, template_name="registration/register.html", context={"register_form":form})
#NO: return render(request, "registration/register.html",{"register_form":form})
我的网站(项目)
mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.contrib.auth.views import LoginView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App1.urls')),
mysite/setting.py
TEMPLATES = [
{
....
#NO: "DIRS" : [ BASE_DIR / "templates" ],
'DIRS': ["templates"],
[
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
在
models.py
我什么也没写。
问题在于您如何呈现注册表单。由于您希望表单位于索引页上,因此不需要单独的视图 (
register_request
)。删除该视图并直接在您的 index
视图中处理表单处理。我在下面的代码片段中做了一些调整。
# In App1/views.py
from .forms import NewUserForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
def index(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registration successful.")
return redirect("index")
else:
messages.error(request, "Unsuccessful registration. Invalid information.")
else:
form = NewUserForm()
return render(request, 'index.html', {'register_form': form})
删除
register_request
中的App1/urls.py
路径:
# In App1/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
这些更改应在您的索引页面上正确显示注册表单。