在base.html
,
<a href="/login">Log In</a>
在主urls.py
,
from ...
from accounts.views import login_view, logout_view, register_view
urlpatterns = [
...
url(r'^login/', login_view),
....
]
在帐户views.py
中,
def login_view (request):
request.session.set_expiry(request.session.get_expiry_age())
form = UserLoginForm(request.POST or None)
title = "Login"
if form.is_valid() :
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(username=username, password=password)
login(request,user)
return HttpResponseRedirect('/')
return render(request, 'accounts/login_form.html',{ "form" : form, "title": title})
点击Log In
后,会在login_form
而不是'registration/login.html'
中搜索'accounts/login_form.html'
模板。
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /Users/***/Desktop/django/mysite/personal/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/***/Desktop/django/mysite/blog/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/***/Desktop/django/mysite/landing/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/***/Desktop/django/mysite/accounts/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Python/2.7/site-packages/crispy_forms/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Python/2.7/site-packages/pagedown/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Python/2.7/site-packages/markdown_deux/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Python/2.7/site-packages/tracking/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Python/2.7/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Python/2.7/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)
所有这一切都正常,直到我尝试过度消除registration/password_reset_form.html
,registration/password_reset_done.html
,registration/password_reset_confirm.html
,
registration/password_reset_complete.html
实现密码重置功能。
主urls.py
已包含,
url(r'^user/password/reset/$','django.contrib.auth.views.password_reset',{'post_reset_redirect' : '/user/password/reset/done/'},name="password_reset"),
url(r'^user/password/reset/done/$','django.contrib.auth.views.password_reset_done'),
url(r'^user/password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$','django.contrib.auth.views.password_reset_confirm',{'post_reset_redirect' : '/user/password/done/'}),
url(r'^user/password/done/$','django.contrib.auth.views.password_reset_complete'),
有什么建议吗?谢谢 !
修改:仅当/login
,/logout
和/register
未用作网址路径时,才会在正确的位置搜索模板。我尝试使用/user_login
,/user_logout
和/user_register
,但它确实有效。我认为它与urls.py
的内置登录相冲突。如何纠正这个?
答案 0 :(得分:1)
您可以为应用urls.py
创建accounts
吗?
帐户urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /accounts/
url(r'^login/', login_view),
]
在主urls.py
from ...
urlpatterns = [
...
url(r'^accounts/', include('yourwebsite.accounts.urls')),
....
]
和base.html
<a href="/accounts/login">Log In</a>
答案 1 :(得分:0)
我上面提到了url(r'^', include('django.contrib.auth.urls'))
login
和其他网址。这就是Django在/registration/login
下寻找模板的原因。当网址为/login
时,我仍然不知道为什么Django会在该位置查找模板。当网址为accounts/login
时,不会出现问题。我把url(r'^', include('django.contrib.auth.urls'))
放在urlpatterns的末尾。
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from accounts.views import login_view, logout_view, register_view, reset_password_view
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls')),
url(r'^', include('personal.urls')),
url(r'^login/', login_view),
url(r'^logout/', logout_view),
url(r'^register/', register_view),
url(r'^reset-password/', reset_password_view),
url(r'^landing/', include('landing.urls')),
url(r'^tracking/', include('tracking.urls')),
url(r'^accounts/', include('accounts.urls')),
url(r'^', include('django.contrib.auth.urls')), # Put this in the end
]