lost password?

home
•  reviewramp
•  rails +
•  javascript
•  webdev
•  django +
•  xaraya
•  xamp
•  musings

rss
Tag this page
   

ยป Blogs that link here
last modified: Mar 26, 2007
(first posted: Mar 12, 2007)
(7183 Reads)
Permalink

Rails and Django - Templates, Forms (part 8/15)

A technical manager's guide to evaluating web development frameworks, with a detailed review of Ruby on Rails and the Django (Python) projects. (Start here: Whitepaper index)

Templates

Obviously both Rails and Django support display templates, as this is a cornerstone of the MVC architecture separating content from presentation. Templates define the layout and formatting of data, for example, into HTML that is sent back to the user's browser in an HTTP response.

In general, templates are intended to be created (or at least edited) by non-programmer designers, an important consideration on team projects. Some debate has ensured regarding just how much programming features should even be permitted in a templating language.

Rails templates let you use the full power of the Ruby language, but you are "strongly encouraged" to use discretion and not put business logic into your templates (move it into the Controllers instead).

Django templates have their own little language that is intentionally limited to simple conditional and iteration control structures, and oriented toward non-programmers (i.e. designers).

Both frameworks permit page layout templates for sharing layouts among views, and specific templates for individual content. In Rails you use "layouts" (parent page), "partials" (included sub-page), and "collections" (an iterated partial). You can write "helper" functions to extend your tags.

Similarly in Django a template can "extend" a parent template (comparable to Rails' layouts but can be multi-level), "inclusions" of sub-pages, and write custom filters and custom tags. In both frameworks you can pass variables to the various template components.

Both frameworks permit you to replace the default templating system with third party alternatives. If you're using Rails but prefer Django's templating language for your designers, for example, someone has written a plugin, named "Liquid" which does just that.

Opinion

It's nice to have the full power of Ruby inside the templates. But I see the point about it being dangerous. And if you're really doing that, the logic should be moved into the controller. Otherwise they both have very good templating features. So I call it even, 3-3.

 

Templates Rails
Django
LanguageRuby Django-template language
Parent layouts "layouts" "extends" directive (bottom up)
Include templates "partials" inclusion tags
Extensions "helpers" custom filters and tags
Can use alternatives?
yes yes
MY RATING: (1=worst, 5=best) 3
3

 

Forms

Basically the role of a framework with regard to forms includes

  • Generate HTML <label> and <input> tags based on Model class attributes
  • Produce those fields in an empty <form> for creating new objects
  • Produce those fields in a <form> with values filled in for updating an existing object
  • Validate values posted by the form based on the Model's validation rules
  • Return to the form, with error messages if any fields do not validate

The logic and syntax may vary somewhat, but both Rails and Django have very good forms support.

Django is presently in the process of rewriting its forms classes to be more flexible. It is available for download from the repository.

Rails includes support for forms that populate multiple Models. Rails also introduces the notion of "form_helpers" that help you standardize the look, layout and behavior of difference forms across your website.

Conclusion: I'll rate forms in Rails and Django equally, 3-3. I can see that Rails' is powerful, and we'll give Django's newforms the benefit of the doubt.

Forms Rails Django
MY RATING: (1=worst, 5=best) 3 3

 


Rails and Django - Templates, Forms (part 8/15)

Posted by: Henrique on September 07, 2008 05:11 PM
You need really to check out Django's 1.0 newforms and then rewrite this part. Writing plugable, reusable forms is great.

#

Rails and Django - Templates, Forms (part 8/15)

Posted by: Mike on January 11, 2009 01:35 PM
Django's template system looks nice but, frankly, overdoes it to the point where you can almost guarantee maintenance and usability issues down the line. Case in point - look at the numerous built in template tags. "Great", you might think, "there are all these tags that let me do all this stuff. I can put all the display logic in the template." Wrong. What happens is beginners get lulled into trying that, and sure it works great for a few forms or simple pages. But as soon as they need to do anything remotely hard, they simply get stuck, cos the template system just isn't up to much. Then they might try implementing their own template tag libraries, and before you know it, they have embarked on writing their own sucky programming language inside the template system. They would be much better off with PHP. In fact I wonder if some of the django developers who left this stuff in have PHPness-envy. Just ask yourself - {% ifequal %}... {% ifnotequal %}? WTF?? {% includes %} in django templates are also deadly. They create highly coupled and fragile designs. Why couple your view code to just one template when you can couple it to two? Even better when you can add in your own crazy template tag scheme to make up for shortcomings in what django's own. It's so confused because the display logic is forced to live in 2 places - inside the view, and also inside the template. No attempt is made to clarify how the display logic should be divided up. Model Template View and claiming that the definition of MVC is debatable just doesn't cut it these days. (http://docs.djangoproject.com/en/dev/faq/general/) The attribute lookup rubbish with {{ x.y.z }} is also deadly in much the same way. It creates fragile designs by ignoring the most fundamental aspect of OOP - encapsulation. Sure, lets rip attributes off stuff at will. Oops we get the schema a bit wrong... guess we'll just have to rewrite those 50 templates we made last months... In short - django would have been far better off leaving all this stuff out and going for a proper OO approach, and being clear about what renders what. Templates should be simple, simple enough for a designer to understand and simple enough that they can't also break things - allowing attribute lookup potentially puts *everything* at the whim of the template. Yup, this means the security nightmare where a designer gets a shot at screwing up your database by using the wrong attribute. We were wondering why the team members seemed to multiply each time we looked at the team list page. It was because someone had used the wrong method as an attribute, and it was re-saving the members on each request. As for newforms - what a mess! Try this: run a find in django/forms/models.py for "inlineformset_factory" (django v1.0). Once you understand what it is doing, you will agree there can be few more convoluted ways of doing what is really not a very complex task. 3 levels of factories for one class? Building the exact same ModelForm classes over and over again on *each and every* request? Having 2 separate classes generate the queryset? Functions taking 15 arguments? django's newforms is textbook bad OO design, python or no python. And please, don't tell me to rewrite it. I was already forced to rewrite a lot of the core code, before our client pulled the plug on our django-powered site entirely, and before we all lost our jobs. Hopefully I will get some time to offer it up to the django gods soon.

#

Rails and Django - Templates, Forms (part 8/15)

Posted by: Izz ad-Din on January 31, 2010 08:06 PM
Wow, you really seem to hate django.

#

Re: Rails and Django - Templates, Forms (part 8/15)

Posted by: Alex N on February 12, 2010 08:03 AM
Wow, so your project failed and you got fired because django's template system is not object oriented enough. Sorry about your job... but really? Lots of sophisticated sites seem to manage to work around these glaring shortcomings.

#

Post a new comment

How many days in a week?

Name :