我正在为项目使用django-registration。我用自己的RegistrationView
覆盖了包的RegistrationViewWithContext
,这使我可以向表单传递额外的上下文。
urlpatterns = [
...
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'^accounts/register$', RegistrationViewWithContext.as_view(form_class=RegistrationFormUniqueEmail), name='registration_register'),
...
]
在localhost:8000/accounts/register
,这很好用。但是现在我要做的是在应用程序的首页/索引上使用相同的形式:localhost:8000/
。此主页上的视图是一个名为dashboard
的功能视图,这是相关部分:
def dashboard(request):
context = {}
current_user = request.user
if current_user.is_authenticated:
# code to show dashboard for already logged-in user
else:
# code to show promo text and registration form
context['form'] = RegistrationFormUniqueEmail()
return render(request, 'dashboard.html', context=context)
dashboard
视图上的表单显示得很好,但是提交表单根本没有任何作用-它仅重定向回首页。我确定我需要在此处添加一个if POST
子句,但是我不确定应该如何构建,因为如上所述,视图对我来说是“隐藏的”,并且位于{{1} }。我有点迷茫。
有什么想法吗?