我遵循这个指南来扩展Django中的注册,这一切似乎都正常,直到我单击寄存器,什么都没有发生。我使用sql签入命令行,根据规范存在表,但是当我尝试使用UserProfile.objects.all()查看条目时,它返回空列表。似乎没有任何东西在表单提交后被发送到任何地方。
我没有错误,所以我有点困惑于什么是问题。
models.py
def user_registered_callback(sender, user, request, **kwargs):
profile = UserProfile(user = user)
profile.first_name = str(request.POST["first_name"])
profile.last_name = str(request.POST["last_name"])
profile.city = str(request.POST["city"])
profile.country = str(request.POST["country"])
profile.save()
user_registered.connect(user_registered_callback)forms.py
from registration.forms import RegistrationForm
class CustomRegistrationForms(RegistrationForm):
first_name = forms.CharField(label ="First Name")
last_name = forms.CharField(label ="Last Name")
city = forms.CharField(label ="City")
country = forms.CharField(label ="Country")urls.py
url(r'^accounts/register/$', RegistrationView.as_view(form_class = CustomRegistrationForms),
name = 'registration_register', kwargs=dict(extra_context={'next_page': '/services/'})),
url(r'^accounts/', include('registration.backends.simple.urls'))
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) registration_form.html
<form method="post" action="" class="wide">
{% csrf_token %}
..sample form
<label for="id_username">Username:</label>
{% if form.username.errors %}
<p class="errors">{{ form.username.errors.as_text }}</p>
{% endif %}
{{ form.username }}
<input type="submit" class="btn btn-default btn-sm" value="Register"></input>
</form>发布于 2014-03-11 06:06:52
从表单标签中完全删除操作。这将导致将表单请求作为HTTP发送到其自身,在本例中是“/accounts/寄存器”。但这不是你的主要问题。
根据文档(https://django-registration.readthedocs.org/en/latest/forms.html),在阅读完之后,我确信您遗漏了您要子类的registration.forms.RegistrationForm所需的字段。后端将处理这些并拒绝给定的值。因为您只是在模板中显示用户名及其错误,因此其他验证错误将永远不会出现。
将所需字段添加到窗体中,然后再试一次。您可能想简单地呈现它。
{{ form.as_p }}https://stackoverflow.com/questions/22312674
复制相似问题