TemplateDoesNotExist at / account /

时间:2017-06-03 03:24:24

标签: python django dynamic-url

我尝试让我的网址更具活力,最终破坏了一些东西。我可以为我的生活弄明白!任何帮助将不胜感激。唯一可用的URL是我的登录和注销模板。我明白为什么这些工作正在发挥作用,但我对其余的网址做错了什么?

这是我收到的追溯:

Traceback (most recent call last):
  File "/Users/m.zayas/Desktop/env1/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/xxxxx/env1/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/xxxxx/env1/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/m.zayas/Desktop/env1/diversely/accounts/views.py", line 20, in home
    return render(request, 'accounts:home')
  File "/xxxxx/env1/lib/python2.7/site-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/xxxxx/env1/lib/python2.7/site-packages/django/template/loader.py", line 67, in render_to_string
    template = get_template(template_name, using=using)
  File "/xxxxx/env1/lib/python2.7/site-packages/django/template/loader.py", line 25, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
TemplateDoesNotExist: accounts:home
[03/Jun/2017 03:15:17] "GET /account/ HTTP/1.1" 500 78337

以下是我的观点:

from django.shortcuts import render, redirect
from django.urls import reverse
from accounts.forms import (
    RegistrationForm,
    EditProfileForm
)
from django.contrib.auth.models import User
from django.contrib.auth.forms import (
    UserChangeForm,
    PasswordChangeForm
)
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required

# Create your views here.

def home(request):
    name = 'Matthew Zayas'
    args = {'myName': name}
    return render(request, 'accounts:home')

def register(request):
    if request.method =='POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse('accounts:home'))
    else:
        form = RegistrationForm()

        args = {'form': form}
        return render(request, 'accounts:register', args)

def view_profile(request):
    args = {'user': request.user}
    return render(request, 'accounts:view_profile', args)

def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)

        if form.is_valid:
            form.save()
            return redirect('accounts:view_profile')
    else:
        form = EditProfileForm(instance=request.user)
        args = {'form': form}
        return render(request, 'accounts:edit_profile', args)

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('accounts:view_profile')
        else:
            return redirect('accounts:change_password')
    else:
        form = PasswordChangeForm(user=request.user)
        args = {'form': form}
        return render(request, 'accounts:change_password', args)

我的网址:

from django.conf.urls import url
from . import views
from django.contrib.auth.views import (
        login,
        logout,
        password_reset,
        password_reset_done,
        password_reset_confirm,
        password_reset_complete
)

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^login/$', login, {'template_name': 'accounts/login.html'}, name='login'),
    url(r'^logout/$', logout, {'template_name': 'accounts/logout.html'}, name='logout'),
    url(r'^register/$', views.register, name='register'),
    url(r'^profile/$', views.view_profile, name='view_profile'),
    url(r'^profile/edit$', views.edit_profile, name='edit_profile'),
    url(r'^change-password/$', views.change_password, name='change_password'),
    url(r'^reset-password/$', password_reset, {'template_name': 'accounts/reset_password.html'}, name='reset_password'),
    url(r'^reset-password/done/$', password_reset_done, name='password_reset_done'),
    url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', password_reset_confirm, name='password_reset_confirm'),
    url(r'^reset-password/complete$', password_reset_complete, name='password_reset_complete')
]

这是我的中间件:

import re

from django.conf import settings
from django.urls import reverse
from django.shortcuts import redirect
from django.contrib.auth import logout

EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]

class LoginRequiredMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        assert hasattr(request, 'user')
        path = request.path_info.lstrip('/')    
        url_is_exempt = any(url.match(path) for url in EXEMPT_URLS)

        if path == reverse('accounts:logout').lstrip('/'):
            logout(request)

        if request.user.is_authenticated() and url_is_exempt:
            return redirect(settings.LOGIN_REDIRECT_URL)
        elif request.user.is_authenticated() or url_is_exempt:
            return None
        else:
            return redirect(settings.LOGIN_URL)

3 个答案:

答案 0 :(得分:0)

您似乎忘记在视图中包含反向功能。

您的代码

def home(request):
name = 'Matthew Zayas'
args = {'myName': name}
return render(request, 'accounts:home')

要做的更改是

def home(request):
name = 'Matthew Zayas'
args = {'myName': name}
return render(request, reverse('accounts:home'))

希望这有帮助。

答案 1 :(得分:0)

render函数需要两个必要的参与者。

来自文档,

  

渲染(request,template_name,context = None,content_type = None,status = None,using = None)[source]

     

将给定模板与给定的上下文字典组合,并返回带有该呈现文本的HttpResponse对象。

     

必填参数

     

请求

     

用于生成此响应的请求对象。

     

<强> TEMPLATE_NAME

     

要使用的模板的全名或模板名称的序列。如果给出了序列,则将使用存在的第一个模板。有关如何找到模板的详细信息,请参阅template loading documentation

因此,您需要在函数中添加 template_name ,而不是url的命名空间。

修改您的观点,

def home(request):
    name = 'Matthew Zayas'
    args = {'myName': name}
    #add the full name of the template and the 
    #context variables you want to add.
    return render(request, 'accounts/home.html', args)

def register(request):
    if request.method =='POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse('accounts:home'))
    else:
        form = RegistrationForm()
    args = {'form': form}
    #here tooo..
    return render(request, 'accounts/register.html', args)

def view_profile(request):
    args = {'user': request.user}
    #same here..
    return render(request, 'accounts/view_profile.html', args)

def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)

        if form.is_valid:
            form.save()
            return redirect('accounts:view_profile')
    else:
        form = EditProfileForm(instance=request.user)
    args = {'form': form}
    return render(request, 'accounts/edit_profile.html', args)

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('accounts:view_profile')
        else:
            return redirect('accounts:change_password')
    else:
        form = PasswordChangeForm(user=request.user)
    args = {'form': form}
    #same thing here too
    return render(request, 'accounts/change_password.html', args)

答案 2 :(得分:0)

转到项目目录上的“ settings.py”文件,然后更改模板“ DIRS”:[]

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates'],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]