Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Django have HTML helpers?

Tags:

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.

like image 349
ejunker Avatar asked Sep 14 '08 16:09

ejunker


People also ask

What is Django Autoescape?

autoescape. Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.

Which characters are illegal in template variable names in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.

What is safe in Django?

Django Templates are safe-by-default, which means that expressions are HTML-escaped by default. However, there are cases where expressions are not properly escaped by default: If your template includes JavaScript, then any expression inside the JavaScript should be JavaScript-escaped and not HTML-escaped.

What does the built in Django template tag Lorem do?

What does the built-in Django template tag "lorem" do? Displays random "lorem ipsum" Latin text Returns true if there are learning objects on the page Displays the number of remaining learning objects Displays the overall number of learning objects 2.


2 Answers

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.

like image 200
saturdayplace Avatar answered Sep 25 '22 08:09

saturdayplace


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.

like image 22
Whatcould Avatar answered Sep 26 '22 08:09

Whatcould