在
index.html
页面(主页)中,我尝试显示一个表单,我这样调用该表单:
{% include 'registration/register.html' %}
我想直接在index.html(
127.0.0.1:8000
)上查看表单,而不是在http://127.0.0.1:8000/register
上查看表单,但问题是在index.html中,我看到蓝色按钮,但看不到文本框(用户名、电子邮件、密码) ):
我做错了什么?有什么问题吗?
这是应用程序1:
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>
.......
.......
{% include 'registration/register.html' %}
</body>
</html>
App1/templates/registration/register.html
{% block content %}
<!--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>
{% endblock %}
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('', 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="registation/register.html", context={"register_form":form})
#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')),
path("login/", LoginView.as_view(), name="login"),
mysite/setting.py
TEMPLATES = [
{
....
#"DIRS" : [ BASE_DIR / "templates" ], #aggiunta io
'DIRS': ["templates"],
[
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
在
models.py
我什么也没写。
谢谢你
您在 App1/urls.py 中定义了两次路径
''
:
from django.urls import path
from . import views
urlpatterns=[
path('', views.index, name='index'),
path('', views.register_request, name='register_request'),
]
第一个似乎优先。因此,如果您访问该 URL,您就会进入索引视图。其中使用index.html模板;但没有定义形式。
index.html 确实包含 Registration/register.html 模板,但没有定义表单,它只渲染 h1 和按钮元素,没有表单。
一种解决方案是定义两条适当的路径,一条作为索引(不包括注册模板),一条用于注册。后者会导致 register_request,但它将是一个相当空的 HTML 页面,因为您只包含 Registration/register.html 模板。
另一个解决方案是仅定义一个索引视图,其中包括表单处理。将所有内容(包括表单上下文)渲染到index.html,您将拥有一个注册页面。但现在你没有非注册索引页。
您可能想要介于两者之间的东西,有两个单独的页面。如何做到这一点取决于您。
您可能想要的是不在index.html中包含注册模板,而是使用index.html扩展注册模板,并覆盖诸如{% block content %}{% endblock %}之类的内容。您已经在 register.html 中定义了这个;现在在index.html中定义它(可能为空)。