tags:

views:

2164

answers:

6

Does Django have any template tags to generate common HTML markup? For example, I know that I can get a url using

{% url mapper.views.foo %}

But that only gives me the URL and not the HTML code to create the link. Does Django have anything similar to Rails' link_to helper? I found django-helpers but since this is a common thing I thought Django would have something built-in.

+2  A: 

it doesnt look like they're built in but here's a couple snippets. it looks like it'd be pretty easy to create these helpers:

http://www.djangosnippets.org/snippets/441/

John Boker
+14  A: 

No it doesn't.

James Bennett answered a similar question a while back, regarding Rails' built-in JavaScript helpers.

It's really unlikely that Django will ever have 'helper' functionality built-in. The reason, if I understand correctly, has to do with Django's core philosophy of keeping things loosely coupled. Having that kind of helper functionality built-in leads to coupling Django with a specific JavaScript library or (in your case) html document type.

EG. What happens if/when HTML 5 is finally implemented and Django is generating HTML 4 or XHTML markup?

Having said that, Django's template framework is really flexible, and it wouldn't be terribly difficult to write your own tags/filters that did what you wanted. I'm mostly a designer myself, and I've been able to put together a couple custom tags that worked like a charm.

saturdayplace
A: 

Here is a list of all template tags and filters built into Django. Django core doesn't have as much HTML helpers as Rails, because Django contributors assumed that web developer knows HTML very well. As stated by saturdaypalace, it's very unlikely for AJAX helpers to be added to Django, because it would lead to coupling Django with a specific JavaScript library.

It's very easy to write your own template tags in Django (often you need just to define one function, similiar to Rails). You could reimplement most of Rails helpers in Django during a day or two.

zuber
A: 

This won't answer directly to the question, but why not using <a href="{% url mapper.views.foo %}">foo</a> in template then?

exalted
+1  A: 

I bet if there would be any consent of what is common html, there would be helpers module too, just for completeness (or because others have it). ;)

Other than that, Django template system is made mostly for HTML people, who already know how to write p, img and a tags and do not need any helpers for that. On the other side there are Python developers, who write code and do not care if the variable they put in context is enclosed by div or by span (perfect example of separation of concerns paradigm). If you need to have these two worlds to be joined, you have do to it by yourself (or look for other's code).

zgoda
+2  A: 

The purpose of helpers is not, as others here imply, to help developers who don't know how to write HTML. The purpose is to encapsulate common functionality -- so you don't need to write the same thing a thousand times -- and to provide a single place to edit common HTML used throughout your app.

It's the same reason templates and SSI are useful -- not because people don't know how to write the HTML in their headers and footers, but sometimes you want to write it just once.

EG. What happens if/when HTML 5 is finally implemented and Django is generating HTML 4 or XHTML markup?

Same thing that happens when HTML 5 is implemented and all your templates are written in repetitive HTML, except a lot easier.

The other posts have already answered the question, linking to the docs on custom template tags; you can use tags and filters to build your own, but no, there aren't any built in.

Whatcould