gpt4 book ai didi

python - django 1.8 CreateView 错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:12 25 4
gpt4 key购买 nike

我正在使用 django 1.8 创建登录表单。但是包含注册表单的模板无法正确呈现。

我的views.py

class SignUpView(CreateView):
RegistrationForm = UserCreationForm
fields = ['username','password1','password2']
model = User
template_name = 'accounts/signup.html'

我的 forms.py

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, ButtonHolder, Submit

class RegistrationForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)

self.helper = FormHelper()
self.helper.layout = Layout(
'username',
'password1',
'password2',
ButtonHolder(
Submit('register', 'Register', css_class='btn-primary')
)
)

signup.html

{% extends 'layouts/base.html' %}
{% load crispy_forms_tags %}

{% block title %}Register | {{ block.super }}{% endblock %}

{% block headline %}<h1>Registration form</h1>{% endblock %}

{% block content %}
{% crispy form %}
{% endblock %}

模板呈现但没有提交按钮。我不知道是什么问题。

最佳答案

您的 SignUpView 没有指定它应该呈现的表单,您需要添加 form_class 属性。

class SignUpView(CreateView):
form_class = RegistrationForm
model = User
template_name = 'accounts/signup.html'

关于python - django 1.8 CreateView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31002468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com