我正在尝试创建用户注册表单。除了表单没有在模板中显示外,它在很大程度上有效。我的基本模板呈现,而不是表单本身。我没有得到追溯错误,页面(除了" base.html"模板)是空白的,即空格,甚至没有按钮。
我尝试重新创建此处完成的教程:https://wsvincent.com/django-user-authentication-tutorial-signup/
signup.html
{% extends 'base.html' %}
{% block title %}Sign Up{% endblock %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.p }}
<button type="submit">Sign up</button>
</form>
{% endblock %}
账户/ urls.py
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
path('signup/', views.SignUp.as_view(), name='signup'),
]
的myproject / urls.py
from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
from clincher import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('clincher/', include('clincher.urls'), name='clincher'),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls'), name='accounts'),
path('', RedirectView.as_view(url='clincher/templates/')),
]
views.py
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic
class SignUp(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
答案 0 :(得分:2)
您的模板中存在问题。
<form method="post">
{% csrf_token %}
{{ form.p }}
<button type="submit">Sign up</button>
</form>
将模板中的代码从上面更改为以下代码:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
当您将其作为form.p
时,模板引擎会搜索名称为&#39; p&#39;的属性。里面&#39;形式&#39;对象,但这不是我们想要的。因此,请使用form.as_p
来呈现包含在段落元素中的表单。