开发者

django reverse() failing

开发者 https://www.devze.com 2023-02-13 11:36 出处:网络
simply put mentions of reverse() anywhere in my project were failing, and so was {% url %}. I have since made some progress if you scroll to the bottom!

simply put mentions of reverse() anywhere in my project were failing, and so was {% url %}. I have since made some progress if you scroll to the bottom!


relevant files

root/urls.py

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

# Uncomment the next two lines to enable the admin:
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'),
)

root/core/views.py

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

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

Notes

I first noticed the reverse() failing when i had a file called site.py in my project that tried to call reverse(). I was using it to store site settings. I was using the file because

  1. I didn't want to use the bother the database with data that would rarely change.
  2. If I nuked my projects database I didn't want my site settings also going down

I have since found a way to use models to achieve the two goals.

but all that is just extra background info, in case you here someone commenting about a site.py.

update 25/02/11

well here goes!

first notice that urls.py has (r'^dbrowse/', include('dbrowse.urls')). that caused reverse() to fail. I'll explain later...

as for the template tag, I've discovered that the {% url %} doesnt take variables. I took this completely for granted.In fact when I was testing the template tag, i'd just go in and hard code something such as {% url 'home' %} which would work and sometimes i'd test {% url home %} with home being a variable. I din't even see this as being completely different test cases.

But i now know {% load url from future %} allows you to use variables as arguments to {% url %}

Anyway, now back to the (r'^dbrowse/', include('dbrowse.urls')) in urls.py

I had a folder like so

project\
    --dbrowse\
     开发者_如何转开发   __init__.py
        urls.py

now this is dbrowse/urls.py

from django.conf.urls.defaults import patterns, url
#databrowse
from django.contrib import databrowse

databrowse.site.register(MyModel)

urlpatterns = patterns('',
                       url(r'(.*)',databrowse.site.root, name='dbrowse'),)

this was my attempt to avoid having to put databrowse.site.register(MyModel) in my project's root urls.py like the docs suggested. I dont like the idea of polluting my projects main urls.py with databrowse.site.register(MyModel)

however I still dont understand why this caused reverse() to break. but i'm supecting it's to do with (.*) being in the pattern.

0

精彩评论

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