My {% url %} tags aren't producing anything -- no error and no url. My
current setup is so bare-bones I can't imagine what's gone wrong. Here
are the basics:
ROOT_URLCONF = Project.urls
In Project.urls:
(r'^$', 'app1.views.index'),
(r'^(?P<browseBy>authors|books|publishers)/$', 'app2.views.browse'),
The app2.views.browse view uses a render_to_response, with a
RequestContext. I've got no TEMPLATE_CONTEXT_PROCESSORS set, so I'm
using default.
app2.views.browse renders the app2/browse.html template, with no
context variables passed in except what the RequestContext puts in
there. I thought the problem was that ROOT_URLCONF wasn't available in
the template, but I imported that specifically and passed it in, with
the same result.
In app2/browse.html template:
<a href="{% url app1.views.index %}">Home</a>
(I've also tried Project.app1.views.index, and other variations, with
the same result)
All I want is a link to the homepage, but nothing is output. This is
the simplest case but the I get the same result in all my views and
templates. I'm using the development version of Django, and the
development server.
Can anyone see where I've got wrong? I tried setting the
TEMPLATE_STRING_IF_INVALID variable to '%s', but I guess this doesn't
actually count as an invalid template tag.
I'd be very grateful for any help!
Eric
Todd
I am learning a lot from the B-List blog
Here is an entry, which could be interesting for you:
http://www.b-list.org/weblog/2007/nov/06/urlconf/
(specially the named URL patterns)
Bernd
Thanks Bernd,
I'd seen that before, and decided I wouldn't mess with it if I
couldn't get the basics to work. But I just tried it, and turning
(r'^$', 'index'),
into
(r'^$', 'index', name="blog-index"),
gave me a syntax error on that line. I'm running r6659, only one short
of head. Is it because I'm not using generic views? I wonder if this
is another indication of whatever it is I've borked...
E
That should be:
url(r'^$', 'index', name="blog-index"),
Jonathan.
No. This one's a case or pilot error. If you want to use this form, you
must write it as:
url(r'^$', 'index', name="blog-index")
url() is a function call, so you can pass it named arguments. The (...)
form (without a leading "url") is a Python tuple and you can't use
'name=value' style arguments and it must have four arguments. Best to
stick to the url() form.
By the way, you can test whether things are working at the interactive
prompt using reverse(), which is how the url template tag is
implemented:
>>> from django.core.urlresolvers import reverse
>>> reverse('index')
u'/'
would be what I would expect to see here. If you want to pass arguments
to reverse(), use the 'args' and 'kwargs' named parameters (the second
positional argument is something different).
Malcolm
--
Depression is merely anger without enthusiasm.
http://www.pointy-stick.com/blog/
Thanks you two, I hadn't caught on that it was going from a tuple to
an actual function.
reverse('index') gets me NoReverseMatch, which isn't surprising given
that my urls aren't working. I'm going to do some tidying and make
sure nothing's escaped me.
I would suggest commenting out every line from your urlpatterns except
the one you are debugging. That way you won't be accidentally sabotaged
by a problem elsewhere.
As has been pointed out here recently, URL configuration either works or
it doesn't: if you have a bug somewhere, such as a non-existent view,
the whole thing falls over. That will be fixed tomorrow, probably -- I'm
going to sit down and spend a day closing bugs and that's on my list.
Malcolm
--
Many are called, few volunteer.
http://www.pointy-stick.com/blog/