tangowithdjango
第12章,作者教我使用django-registration-redux
来实现登录和注册功能。但我发现它只是不起作用。这意味着,我无法使用它来登录和注册。我只是按照作者告诉我的步骤进行操作。
这是我的 urls.py
from django.conf import settings
from django.conf.urls import include, url, patterns
from django.conf.urls.static import static
from django.contrib import admin
from registration.backends.simple.views import RegistrationView
# Create your views here.
class MyRegistrationView(RegistrationView):
def get_success_url(self, request, user):
return '/search/'
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
url(r'^search/', include('search_engine.urls')),
url(r'^accounts/', include('registration.backends.simple.urls')),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
setting.py
INSTALLED_APPS = (
'registration',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'django_comments',
'django.contrib.sitemaps',
)
REGISTRATION_OPEN = True
ACCOUNT_ACTIVATION_DATS = 7
REGISTRATION_AUTO_LOGIN = True
LOGIN_REDIRECT_URL = '/search/'
LOGIN_URL = '/accounts/login/'
我还有三个html文件,包括templates/account/base.html
,templates/registration/login.html
和templates/registration/registration_form.html
我的login.html
<form method="post" role="form" class="form-signin" action=".">
{% csrf_token %}
<div class="form-group">
<div class="input-group border" style="width: 300px">
<label for="id_username" class="sr-only">用户名</label>
<input class="form-control border" style="border-top-width: medium;" id="id_username" width="200px" maxlength="35" name="username" type="text" placeholder="请输入您的用户名" />
<label for="id_password" class="sr-only">密码</label>
<input class="form-control border" style="border-bottom-color: #f49f00;" id="id_password" name="password1" type="password" placeholder="请输入您的密码" />
</div>
<div class="input-group-btn">
<button class="btn btn-default" style="margin: 4% 0 0 0; width: 300px; border-radius: 2px;background-color: #1a274b; color: white; " type="submit">登录</button>
</div>
</div>
</form>
答案 0 :(得分:0)
您的模板太复杂了。从template from the tutorial开始,先使其工作。
{% extends "rango/base.html" %}
{% block body_block %}
<h1>Login</h1>
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Log in" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p>Not a member? <a href="{% url 'registration_register' %}">Register</a>!</p>
{% endblock %}
然后,一旦视图正常工作,您可以根据需要自定义模板。请记住包含您目前没有执行的表单错误。有关详细信息,请参阅rendering fields manually上的文档。