我跟着这个guide来扩展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>
答案 0 :(得分:1)
完全从表单标记中删除操作。这将导致将表单请求作为HTTP-POST发送给自己,在本例中为“/ accounts / register”。但这不是你的主要问题。
根据文档(https://django-registration.readthedocs.org/en/latest/forms.html)阅读后我很确定你缺少registration.forms.RegistrationForm所要求的字段,你正在进行子类化。后端将处理这些并拒绝给定的值。由于您只在模板中显示用户名及其错误,因此其他验证错误将永远不会显示。
将必填字段添加到表单中,然后重试。您可能只想通过
进行渲染{{ form.as_p }}