开发者

django url reverse not working for named urls (re-edited) found the cause but now stuck!

开发者 https://www.devze.com 2023-02-12 09:55 出处:网络
See bottom for latest break throughs: I\'m running django SVN 15632 Tried viewHallo in module core.views. Error was: \'module\' object has no attribute \'viewHallo\'

See bottom for latest break throughs: I'm running django SVN 15632

Tried viewHallo in module core.views. Error was: 'module' object has no attribute 'viewHallo'

is the error I am getting after trying reverse('home') or reverse('admin:index').

this is my projects urls.py:

from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.views import serve as serveStatic

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/',   include(admin.site.urls)),
    (r'^dbrowse/', include('dbrowse.urls')),
    (r'^static/',  serveStatic),
url (r'^$',    'core.views.viewHallo',name='home'),
)

this is core\views.py

from django.shortcuts import render_to_response
from django.template.context import RequestContext
import site

def viewHallo (request):
    pass
    return render_to_response ('core.html',
                               {'site':site,
                                'title':'i am the hallo view',
                                'content':'Hallo World!',},
                                context_instance=RequestContext(request))

using the shell or even a script reverse('home') and reverse('admin:index') just isn't working.

However in my templates {% url home %} and {% url admin:index %} work just fine...

in my app core i have a file called site.py i use this file to store stuff about the site so i dont have to rely on the database. long story short it contains reverse('home'). Now this is only important because no matter where reverse() is excecuted script or shell or template, the stack trace always includes the line from site.py.

why on earth is django excecuting site.py? and even if it does why is it stumbling over the reverse('home')?. Interstingly if I comment out the line from site.py then reverse() starts working properly.

whats going on?? here is core\sit开发者_C百科e.py

from django.contrib.sites.models import Site
from django.conf import settings
from django.core.urlresolvers import reverse

site = Site.objects.get(pk=settings.SITE_ID)

NAME = site.name
SLOGAN = 'it\'s a deal!'
COPY_HOLDER = 'My Name'


#(link_title, 'link_address', ['permission'])
MAIN_MENU = [['home', reverse('home'), 'core.view_tender'],
             ['admin', reverse('admin:index'), 'is_staff']]

EDIT: I have isolated the line in django's source code that's throwing the error: line 91 in django/core/urlresolvers.py

91: lookup_view = getattr(import_module(mod_name), func_name) it triggers django importlib which in turn imports site.py.


There are a few problems here.

reverse('home') should work exactly how you have it but your error is happening before reverse can complete. We've got to sort out the URL conf issues first.

Does core.views.viewHallo actually exist?


For the admin urls, name= will not work because it is an include (which doesn't have a specific url, it just points to another url conf, so reverse('admin') wouldn't work).

The admin urls have a namespace, and namespaced urls are reversed with namespace:named_url http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

You say it's the only thing that works -- what else are you trying to do? To reverse other admin urls, there is a specific syntax outlined here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

url = reverse('admin:app_model_change')

Let me know if you have any questions.


The problem is that you are importing the site module before the function viewHallo is defined. The error message is quite correct - at the time reverse is being called, the module has no such member viewHallo. Normally, the module would be loaded up when needed and everything would be fine, but in this case, the module is already in the process of being loaded. It's a case of having a cyclic dependency between Python modules.

0

精彩评论

暂无评论...
验证码 换一张
取 消