Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango reverse() failing
    text
    copied!<p>simply put mentions of <code>reverse()</code> anywhere in my project were failing, and so was {% url %}. I have since made some progress if you scroll to the bottom!</p> <hr> <p><strong>relevant files</strong></p> <p><strong>root/urls.py</strong></p> <pre><code>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'), ) </code></pre> <p><strong>root/core/views.py</strong></p> <pre><code>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)) </code></pre> <hr> <p><strong>Notes</strong></p> <p>I first noticed the <code>reverse()</code> failing when i had a file called <code>site.py</code> in my project that tried to call <code>reverse()</code>. I was using it to store site settings. I was using the file because</p> <ol> <li>I didn't want to use the bother the database with data that would rarely change.</li> <li>If I nuked my projects database I didn't want my site settings also going down</li> </ol> <p>I have since found a way to use models to achieve the two goals.</p> <p>but all that is just extra background info, in case you here someone commenting about a <code>site.py</code>.</p> <p><strong>update 25/02/11</strong></p> <p>well here goes!</p> <p>first notice that <code>urls.py</code> has <code>(r'^dbrowse/', include('dbrowse.urls'))</code>. that caused <code>reverse()</code> to fail. I'll explain later...</p> <p>as for the template tag, I've discovered that the <code>{% url %}</code> 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 <code>{% url 'home' %}</code> which would work and sometimes i'd test <code>{% url home %}</code> with <code>home</code> being a variable. I din't even see this as being completely different test cases.</p> <p>But i now know <code>{% load url from future %}</code> allows you to use variables as arguments to <code>{% url %}</code></p> <p>Anyway, now back to the <code>(r'^dbrowse/', include('dbrowse.urls'))</code> in <code>urls.py</code></p> <p>I had a folder like so</p> <pre><code>project\ --dbrowse\ __init__.py urls.py </code></pre> <p>now this is <strong>dbrowse/urls.py</strong></p> <pre><code>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'),) </code></pre> <p>this was my attempt to avoid having to put <code>databrowse.site.register(MyModel)</code> in my project's root <code>urls.py</code> like the docs suggested. I dont like the idea of polluting my projects main <code>urls.py</code> with <code>databrowse.site.register(MyModel)</code></p> <p>however I still dont understand why this caused <code>reverse()</code> to break. but i'm supecting it's to do with <code>(.*)</code> being in the pattern.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload