python 2.7 - Adding attributes to form labels in Django 1.5 -


I have some code that looks like:

  # form.html & lt; Form action = "/ form-action" method = "post" & gt; {{Form.faction.label_tag}} {{form.faction}} & lt; Br / & gt; {{Form.character_name.label_tag}} {{form.character_name}} & lt; / Form & gt; #Form.py class characterform: function_OICES = (('SIth', 'Sith Empire'), ('Republic', 'Republic Alliance')) group = form. Chargefield (option = function_choose) character_name = form .CharField (max_length = 100)   

Although I have to be a specified square on the label tags on the HTML output. I know I can:

  & lt; Form action = "/ form-action" method = "post" & gt; & Lt; Label = "id_faction" class = "foo" & gt; For {{form.faction.label}}: & lt; / Label & gt; {{Form.faction}} & lt; Br / & gt; & Lt; Label = "id_character_name" class = "bar" & gt; For {{form.character_name.label}}: & lt; / Label & gt; {{Form.character_name}} & lt; / Form & gt;   

I used to first rail and you can do something with the following:

  <% = f.label: field, class : "Baz" & gt%;   

and was hoping that it was as easy for a solution in the Djangogo. I thought using form.field-name.label_tag (attrs = {'class': 'custom-class-name'}) was going to work out but alas that I'm stuck Any insights is appreciated.

I always use it for

  1. Install it: Install widget-tweaks.
  2. Add the 'widget_tweaks' to the INSTALLED_APPS immediately after the App Dezas in App settings and just before your app.
  3. Add the {% load widget_tweaks%} template to the top of the template you will customize.

    Now to add a class for form1 to a field of " name ":

      {{Form1.name | Add_class: "baz"}}   

    or add 2 more

      {{Form1.name | Add_class: "baz baz2 baz3"}}   

    which will add class = "baz baz2 baz3" to your field HTML output

    Attributes can also be added:

      {{form1.name | Attr: "size: 200" | Attr: Combine "color: blue"}}   

    and two ^^

      {{form1.name | Add_class: "baz baz2 baz3" | Att: "Size: 200" | You can also test the field type for a customized output by i. I do not include the snippet according to the type type: "color: blue"}}   

    This works for me on Django 1.6 and 1.7 what I do, please consult the docs of widget_tweaks. This will give you

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -