2009-07-03 52 views
5

在項目中的Django命名的網址/ urls.py我:通過模板URL標記無法識別

urlpatterns = patterns('bizteen.projects.views', 
    url(r'^browse/$', 'browse', name='projects-browse-main'), 
    url(r'^browse/(\d+)/$', 'browse', name='projects-browse'), 
    url(r'^create/$', 'create', name='projects-create'), 
    url(r'^(\d+)/$', 'view_project', name='projects-view'), 
) 

而且在模板中,我有:

<a href="{% url projects-browse-main %}">Browse projects</a> 

但錯誤出現。

TemplateSyntaxError at/

Caught an exception while rendering: Reverse for 'bizteen.projects-browse-main' with arguments '()' and keyword arguments '{}' not found. 

我能弄清的最多的是名稱被視爲像視圖一樣。爲什麼會發生?

from django.shortcuts import render_to_response 
from django.http import HttpResponse, HttpResponseRedirect 
from bizteen.projects.models import Project, ProjectComment 
from django.template import RequestContext 

def browse(request, page=0): 
    page_start = page * 25 
    page_end = page_start + 25 
    project_list = Project.objects.order_by('date_created').reverse([page_start:page_end] 
    return render_to_response('browse_projects.html', {'project_list': project_list}, context_instance=RequestContext(request)) 

和主urls.py:

從bizteen.projects.views

from django.conf.urls.defaults import * 

from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    (r'^users/', include('bizteen.userthings.urls')), 
    (r'^projects/', include('bizteen.projects.urls')), 
    url(r'^$', 'bizteen.userthings.views.home', name='main-home'), 

    url(r'^profile/$', 'bizteen.userthings.user-profile'), 

    url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
    url(r'^admin/(.*)', admin.site.root), 

    url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', 
     {'document_root': 'templates/static'}), 
) 

回溯:

Environment: 

Request Method: GET 
Request URL: http://127.0.0.1:8000/ 
Django Version: 1.0.2 final 
Python Version: 2.5.1 
Installed Applications: 
['django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.admin', 
'bizteen.userthings', 
'bizteen.projects'] 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware') 


Template error: 
In template /Users/xenon/bizteen/trunk/web/bizteen/templates/base.html, error at line 33 
    Caught an exception while rendering: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found. 
    23 :   <div class="span-24"> 


    24 :     <div class="span-10">&nbsp; </div> 


    25 :     <div class="span-8"> 


    26 :      <div id='quick_user_menu'> 


    27 :       <b>Hi there!</b> 


    28 :       {% if user.is_authenticated %} 


    29 :       <b><a href="{{ user.get_absolute_url }}">{{ user.username }}</a></b> 


    30 :        <a href='/user/home/' class='lightblue'>Dashboard</a> | 


    31 :        (<a href='/user/logout/' class='anchor'>logout</a>) 


    32 :       {% else %} 


    33 :       <a href=' {% url main-home %} ' class='lightblue' rel='facebox'>Sign In</a> 


    34 :       (<a href='/user/signup/' class='anchor'>Don't have an account?</a>) 


    35 :       


    36 :       {% endif %} 


    37 :      </div> 


    38 :     </div> 


    39 :   </div> 


    40 :   <div class="span-24"> 


    41 :    <div id='header'> 


    42 :     <div class="span-8"> 


    43 :      <A href='/user/home/'><img src='http://localhost:8000/site_media/images/logo.png' alt='BizTeen logo image' align='left'/></a> 


Traceback: 
File "/Library/Python/2.5/site-packages/django/core/handlers/base.py" in get_response 
    86.     response = callback(request, *callback_args, **callback_kwargs) 
File "/Users/xenon/bizteen/trunk/web/bizteen/../bizteen/userthings/views.py" in home 
    29.  return render_to_response('home.html', context_instance=RequestContext(request)) 
File "/Library/Python/2.5/site-packages/django/shortcuts/__init__.py" in render_to_response 
    18.  return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) 
File "/Library/Python/2.5/site-packages/django/template/loader.py" in render_to_string 
    107.  return t.render(context_instance) 
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render 
    176.   return self.nodelist.render(context) 
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render 
    768.     bits.append(self.render_node(node, context)) 
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node 
    71.    result = node.render(context) 
File "/Library/Python/2.5/site-packages/django/template/loader_tags.py" in render 
    97.   return compiled_parent.render(context) 
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render 
    176.   return self.nodelist.render(context) 
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render 
    768.     bits.append(self.render_node(node, context)) 
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node 
    71.    result = node.render(context) 
File "/Library/Python/2.5/site-packages/django/template/defaulttags.py" in render 
    246.    return self.nodelist_false.render(context) 
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render 
    768.     bits.append(self.render_node(node, context)) 
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node 
    81.    raise wrapped 

Exception Type: TemplateSyntaxError at/
Exception Value: Caught an exception while rendering: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found. 

Original Traceback (most recent call last): 
    File "/Library/Python/2.5/site-packages/django/template/debug.py", line 71, in render_node 
    result = node.render(context) 
    File "/Library/Python/2.5/site-packages/django/template/defaulttags.py", line 378, in render 
    args=args, kwargs=kwargs) 
    File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 254, in reverse 
    *args, **kwargs))) 
    File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 243, in reverse 
    "arguments '%s' not found." % (lookup_view, args, kwargs)) 
NoReverseMatch: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found. 

回答

2

從我的urls.py:

url(r'^profile/$', 'bizteen.userthings.user-profile'), 

這種觀點是不存在的。我覺得很愚蠢。

3

我想可能是因爲

url(r'^browse/$', 'browse', name='projects-browse-main'), 
url(r'^browse/(\d+)/$', 'browse', name='projects-browse'), 

都指向'bizteen.projects.views'中的相同函數'browse',但其中一個參數需要參數(\ d +),另一個不參與。你有沒有爲函數指定一個默認參數?像這樣

def browse(request, project="1"): 

如果不是,則可能是在您的項目範圍內的urls.py的問題(​​你的例子看起來像是從一個應用程序urls.py摘錄)。你在項目範圍的urls.py中有這樣的事嗎?

urlpatterns = patterns('', 
    (r'^project/', include('bizteen.project.urls)) 
) 
+0

是的,我在那裏指定了默認值。此外,該問題適用於我的所有網址,而不僅僅是那些共享視圖的網址。 – OwenK 2009-07-03 20:47:22

2

我personnaly導入我的urls.py我的觀點,讓他們爲URL的第二個參數()fonctions:

from myapp import views as myapp_views 

urlpatterns = patterns('', 
    url("^myapp/blah/$", myapp_views.blah, name="myapp_blah"), 
) 

它避免讓運行時錯誤,並允許您添加裝飾像「login_required」:

from myapp import views as myapp_views 

urlpatterns = patterns('', 
    url("^myapp/blah/$", login_required(myapp_views.blah), name="myapp_blah"), 
)