Be the first to know about special offers and new products for web developers and Internet business owners.
I will never share your email address with the bad boys. Period.
|
Be the first to know about special offers and new products for web developers and Internet business owners. I will never share your email address with the bad boys. Period.
The latest
Recent comments
|
Passing variables to named patterns via {% url %} in Django templates
Submitted by alexis on Mon, 2008-10-13 20:44.
When coding Django one of my most important goals is avoiding all kind of hardcoded urls in my applications. Let's see this line from one of my app's URLConf:
This points to a page like /edit/object/123, notice I'm using a named pattern here, and calls a view defined with:
Then I can do what I need with the object identified by object_id. Now, imagine I have a list of objects in a template so I can add an edit link to each one of them, something like this:
Notice that 4? I put it there just for making my point clear, what I really want to do is something like:
but that produces an error like this: TemplateSyntaxError at ... Could not parse the remainder: '{{' from '{{'. It seems a template variable can't be used inside a {% url %} tag, and I guess the same applies to any template tag. After thinking a bit about how other tags work with variables, the {% for %} loop for example, I decided to get rid of the {{ and }}:
And yep, that worked. I hope this helps anybody else having a similar question. Trackback URL for this post:http://www.ventanazul.com/webzine/trackback/120
|
Want to launch a online store, need a new look for your website, market your services on the Net or build a virtual community? Not a problem, let's talk about it. Where To Host Your Sites? |
Post new comment