Django在未指定的位置搜索模板

时间:2017-02-16 10:25:35

标签: python django django-models django-templates django-admin

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.htmlregistration/password_reset_done.htmlregistration/password_reset_confirm.htmlregistration/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的内置登录相冲突。如何纠正这个?

2 个答案:

答案 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 
]