2014-10-19 70 views
0

reverse('main_page')作品在Django殼裝飾觀點引起ViewDoesNotExist錯誤

$ python manage.py shell 
In [1]: import auth_lifecycle.view__profile 
In [2]: from django.core.urlresolvers  import reverse 
In [3]: reverse('main_page') 
Out[3]: '/auth_lifecycle/' 

但在取消對login_required裝飾在這個視圖文件

from django.contrib.auth.decorators import login_required 
from django.core.urlresolvers  import reverse 
from django.shortcuts    import render 
from django.template    import RequestContext 

#@login_required(login_url=reverse('main_page')) 
def get_rendered(request): 
    return render(request, 'auth_lifecycle/user_profile.html', 
        context_instance=RequestContext(request)) 

然後重新啓動外殼,並試圖

import auth_lifecycle.view__profile 

原因

ViewDoesNotExist: Could not import auth_lifecycle.view__profile.get_rendered. View does not exist in module auth_lifecycle.view__profile.

爲什麼?


完全回溯:

In [1]: import auth_lifecycle.view__profile 
--------------------------------------------------------------------------- 
ViewDoesNotExist       Traceback (most recent call last) 
<ipython-input-1-f8b0669189a9> in <module>() 
----> 1 import auth_lifecycle.view__profile 

/home/jeffy/django_files/django_auth_lifecycle/auth_lifecycle/view__profile.py in <module>() 
    19 #After the login view is created, change this to 'login': 
    20 PROFILE_LOGGED_OUT_REDIRECT_URL_NAME='main_page' 
---> 21 reverse('main_page') 
    22 @login_required(login_url='http://usatoday.com') 
    23 #@login_required(login_url=reverse(PROFILE_LOGGED_OUT_REDIRECT_URL_NAME)) 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in reverse(viewname, urlconf, args, kwargs, prefix, current_app) 
    544    resolver = get_ns_resolver(ns_pattern, resolver) 
    545 
--> 546  return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) 
    547 
    548 reverse_lazy = lazy(reverse, str) 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs) 
    408 
    409   if not self._populated: 
--> 410    self._populate() 
    411 
    412   try: 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in _populate(self) 
    290     else: 
    291      parent_pat = pattern.regex.pattern 
--> 292      for name in pattern.reverse_dict: 
    293       for matches, pat, defaults in pattern.reverse_dict.getlist(name): 
    294        new_matches = normalize(parent_pat + pat) 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in reverse_dict(self) 
    313   language_code = get_language() 
    314   if language_code not in self._reverse_dict: 
--> 315    self._populate() 
    316   return self._reverse_dict[language_code] 
    317 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in _populate(self) 
    301    else: 
    302     bits = normalize(p_pattern) 
--> 303     lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args)) 
    304     if pattern.name is not None: 
    305      lookups.appendlist(pattern.name, (bits, p_pattern, pattern.default_args)) 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in callback(self) 
    229    return self._callback 
    230 
--> 231   self._callback = get_callable(self._callback_str) 
    232   return self._callback 
    233 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/functools.py in wrapper(*args, **kwds) 
    426      hits += 1 
    427      return result 
--> 428     result = user_function(*args, **kwds) 
    429     cache[key] = result 
    430     misses += 1 

/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site-packages/django/core/urlresolvers.py in get_callable(lookup_view, can_fail) 
    116      raise ViewDoesNotExist(
    117       "Could not import %s. View does not exist in module %s." % 
--> 118       (lookup_view, mod_name)) 
    119  return lookup_view 
    120 

ViewDoesNotExist: Could not import auth_lifecycle.view__profile.get_rendered. View does not exist in module auth_lifecycle.view__profile. 

回答

0

更改reversereverse_lazy修復它。

from django.contrib.auth.decorators import login_required 
from django.core.urlresolvers  import reverse_lazy 
from django.shortcuts    import render 
from django.template    import RequestContext 

@login_required(login_url=reverse_lazy('main_page')) 
def get_rendered(request): 
    return render(request, 'auth_lifecycle/user_profile.html', 
        context_instance=RequestContext(request)) 

只導入視圖文件意味着URL尚未加載。 reverse要求網址已被加載。 reverse_lazy沒有。