Django Inline Forms

In Django sometimes it’s more practical to have a specific HTML inline form than working with the CSS.
Django makes it particular easy. All one has to do is to define an as_inline function that uses <span> instead of <p> or <ul>/<li>.

Here’s a simple example of a form that has a text search and a country drop down list.
Note that the row ender is a combination of three blank spaces. This is obviously practical but not necessarily the best approach.

# Filter country
class FilterCountry(forms.Form):
        Filter country

    lsearch = ugettext("Search")    
    lcountry = ugettext("Only from country")
    lcountry_empty = ugettext('All')
    search_term = forms.CharField(required=False, label=lsearch)
    country = forms.ModelChoiceField(queryset=Country.objects.all(), required=False, empty_label=lcountry_empty, label=lcountry)
    def as_inline(self):
         """Returns this form rendered as HTML inlines."""
         return self._html_output(
                normal_row = u'<span%(html_class_attr)s>%(label)s %(field)s%(help_text)s</span>',
                error_row = u'%s',
                row_ender = ' &nbsp; ',
                help_text_html = u' %s',
                errors_on_separate_row = True)