django注册,注册和登录表单在索引页面一起,我做的对吗? [英] django-registration, registration and login form on index page together, did I do it right?
问题描述
它的工作原理,但我觉得这很黑非常优雅,而且还有另一种适当的方式来做到这一点。例如,我觉得在注册和auth中调用或扩展视图方法可能更好,而不是复制粘贴它们。
def index (request,success_url = None,
form_class = RegistrationForm,
authentication_form = AuthenticationForm,
profile_callback = None,
template_name ='index.html',
extra_context = None ,** kwargs):
redirect_to = request.REQUEST.get('next','')
如果request.method =='POST':
form = form_class(data = request.POST,files = request.FILES)
form_auth = authentication_form(data = request.POST)
如果form.is_valid():
new_user = form.save(profile_callback = profile_callback)
#success_url需要在这里动态生成;使用reverse()设置
#的默认值将导致循环导入
#与此应用程序的默认URLConf的问题,
#导入此文件。
return HttpResponseRedirect(success_url or reverse('registration_complete'))
如果form_auth.is_valid():
netloc = urlparse.urlparse(redirect_to)[1]
#如果redirect_to为空,则使用默认设置
如果不是redirect_to:
#redirect_to = settings.LOGIN_REDIRECT_URL
redirect_to =/
#安全检查 - 不允许重定向到不同的
#主机。
elif netloc和netloc!= request.get_host():
#redirect_to = settings.LOGIN_REDIRECT_URL
redirect_to =/
#好的,安全检查完成。记录用户
auth_login(request,form_auth.get_user())
如果request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect(redirect_to)
else:
form = form_class()
form_auth = authentication_form()
如果extra_context为None:
extra_context = {}
context = RequestContext(request)
用于key,extra_context.items()中的值:
context [key] = callable )和value()或值
返回render_to_response(template_name,
{'form':form,'form_auth':form_auth},
context_instance = context)
和index.html中的表单:
{%if form.errors%}
< p class =errors>请更正以下错误:{{form.non_field_e rrors}}< / p>
{%endif%}
< h3>创建一个帐户< / h3>
< form method =postaction =class =wide>
{%csrf_token%}
< p>
< label for =id_username>您的用户名:< / label>
{%if form.username.errors%}
< p class =errors> {{form.username.errors.as_text}}< / p>
{%endif%}
{{form.username}}
< / p>
< p>
< label for =id_email>电子邮件地址:< / label>
{%if form.email.errors%}
< p class =errors> {{form.email.errors.as_text}}< / p>
{%endif%}
{{form.email}}
< / p>
< p>
< label for =id_password1>密码:< / label>
{%if form.password1.errors%}
< p class =errors> {{form.password1.errors.as_text}}< / p>
{%endif%}
{{form.password1}}
< / p>
< p>
< label for =id_password2>>密码(再次键入拼写错误):< / label>
{%if form.password2.errors%}
< p class =errors> {{form.password2.errors.as_text}}< / p>
{%endif%}
{{form.password2}}
< / p>
< p class =submit>< input type =submitvalue =Register>< / p>
< / form>
{%if form_auth.errors%}
< p class =error>请更正以下错误:< / p>
{%endif%}
< h3>登录< / h3>
< form method =postaction =?next = {{next | default:/}}>
{%csrf_token%}
< dl>
< dt>< label for =id_username>用户名:< / label> {%if form.username.errors%}< span class =error> {{form.username .errors | join:,}}< / span> {%endif%}< / dt>
< dd> {{form_auth.username}}< / dd>
< dt>< label for =id_password>密码:< / label> {%if form.password.errors%}< span class =error> {{form.password .errors | join:,}}< / span> {%endif%}< / dt>
< dd> {{form_auth.password}}< / dd>
< dt>< input type =submitvalue =登录/>< / dt>
< / dl>
< / form>
在索引中放置登录或注册表格很自然页面(或每个页面),但为什么需要处理表单?在 / auth / login /
上进行登录,在 / auth / registration /
上进行注册,您的代码将是干净的可扩展。
I'd appreciate some code review, I used django-registration app and django.contrib.auth module. What I wanted to do is have both the login and registration form on the index page, and manage it from there. What I did is I just copied code from registration.views.py and contrib.auth.views.py and banged it together.
It works but I feel it's very hack-ish, non elegant, and that there is another, proper way to do it. For example I feel it might be better to call or extend view methods in registration and auth instead of copy pasting them.
def index(request, success_url=None,
form_class=RegistrationForm,
authentication_form=AuthenticationForm,
profile_callback=None,
template_name='index.html',
extra_context=None, **kwargs):
redirect_to = request.REQUEST.get('next', '')
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
form_auth = authentication_form(data=request.POST)
if form.is_valid():
new_user = form.save(profile_callback=profile_callback)
# success_url needs to be dynamically generated here; setting a
# a default value using reverse() will cause circular-import
# problems with the default URLConf for this application, which
# imports this file.
return HttpResponseRedirect(success_url or reverse('registration_complete'))
if form_auth.is_valid():
netloc = urlparse.urlparse(redirect_to)[1]
# Use default setting if redirect_to is empty
if not redirect_to:
#redirect_to = settings.LOGIN_REDIRECT_URL
redirect_to = "/"
# Security check -- don't allow redirection to a different
# host.
elif netloc and netloc != request.get_host():
#redirect_to = settings.LOGIN_REDIRECT_URL
redirect_to = "/"
# Okay, security checks complete. Log the user in.
auth_login(request, form_auth.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect(redirect_to)
else:
form = form_class()
form_auth = authentication_form()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{ 'form': form, 'form_auth': form_auth},
context_instance=context)
And forms in the index.html:
{% if form.errors %}
<p class="errors">Please correct the errors below: {{ form.non_field_errors }}</p>
{% endif %}
<h3>Create an account</h3>
<form method="post" action="" class="wide">
{% csrf_token %}
<p>
<label for="id_username">Your Username:</label>
{% if form.username.errors %}
<p class="errors">{{ form.username.errors.as_text }}</p>
{% endif %}
{{ form.username }}
</p>
<p>
<label for="id_email">Email address:</label>
{% if form.email.errors %}
<p class="errors">{{ form.email.errors.as_text }}</p>
{% endif %}
{{ form.email }}
</p>
<p>
<label for="id_password1">Password:</label>
{% if form.password1.errors %}
<p class="errors">{{ form.password1.errors.as_text }}</p>
{% endif %}
{{ form.password1 }}
</p>
<p>
<label for="id_password2">Password (type again to catch typos):</label>
{% if form.password2.errors %}
<p class="errors">{{ form.password2.errors.as_text }}</p>
{% endif %}
{{ form.password2 }}
</p>
<p class="submit"><input type="submit" value="Register"></p>
</form>
{% if form_auth.errors %}
<p class="error">Please correct the errors below:</p>
{% endif %}
<h3>Log in</h3>
<form method="post" action="?next={{ next|default:"/" }}">
{% csrf_token %}
<dl>
<dt><label for="id_username">Username:</label>{% if form.username.errors %} <span class="error">{{ form.username.errors|join:", " }}</span>{% endif %}</dt>
<dd>{{ form_auth.username }}</dd>
<dt><label for="id_password">Password:</label>{% if form.password.errors %} <span class="error">{{ form.password.errors|join:", " }}</span>{% endif %}</dt>
<dd>{{ form_auth.password }}</dd>
<dt><input type="submit" value="Log in" /></dt>
</dl>
</form>
It's quite natural to place login or registration form at index page (or on every page), but why do you need to process the forms there? Process login on /auth/login/
, process registration on /auth/registration/
and your code will be clean and extendable.
这篇关于django注册,注册和登录表单在索引页面一起,我做的对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!