EzDevInfo.com

django-widget-tweaks

Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

how to add css class to radio button in django template

I am trying to do the following with django widget tweaks:

{{ form.gender.0.tag|attr:"class:radio_1" }}

I get the error:

'SafeText' object has no attribute 'as_widget'

What am I doing wrong?


Source: (StackOverflow)

django apply widget for all fields

Is there a way to apply css class to ALL fields instead of doing the following for every single field.
forms.py

class UserColorsForm(forms.ModelForm):

    class Meta:
        model = UserColors
        exclude = ('userid',)
        widgets = {
            'text_color': forms.TextInput(attrs={'class': 'color'}),
            'background_color': forms.TextInput(attrs={'class': 'color'}),
            ... 10 more
        }

Source: (StackOverflow)

Advertisements

Django custom form template and help text

I'm trying iterate over form fields, and not want to use default {{ field }} tag. I want customise each field in cycle.

{% for field in wizard.form %}
<div class="row">
        <div class="small-8 columns">
            <label for="id_{{ field.html_name }}"class="inline{% if field.errors %}error {% endif %}">
                {{ field.label }}
            </label>

        </div>
        <div class="small-4 columns">

            {{ field|add_error_class:"error" }}

            {% if field.errors %}
                <small class="error">{{ field.errors.as_text }}</small>
            {% endif %}
        </div>
    </div>
{% endfor %}

I want to use something instead

{{ field|add_error_class:"error" }}.

Renders to:

<input class="timepicker" id="id_1-begin_time" name="1-begin_time" type="text" value="01:30:00" />

I want:

<input class="**{{ field.class }}**" id="id_{{ field.html_name }}" name="{{ field.html_name }}" type="**{{ field.type }}**" value="{{ field.value }}" />

Source: (StackOverflow)

TemplateSyntaxError: widget_tweaks is not a valid library django

I am new to django and I am trying to trying to implement a form that uses widget-tweaks.

I did install widget tweaks (I am using Ubuntu 14.04)

sudo pip install django-widget-tweaks

My settings file looks like this:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'addattr',
'widget_tweaks',
)

The html looks like this:

{% extends "base/theme.html" %}
{% load widget_tweaks %}
{% block main_content %}
...more code...

The class in the views and the linking in the url works perfectly fine.

But every time when I try to load the html it says:

widget_tweaks' is not a valid tag library: Template library widget_tweaks not found, tried django.templatetags.widget_tweaks,django.contrib.admin.templatetags.widget_tweaks,django.contrib.staticfiles.templatetags.widget_tweaks

Can anyone help? Thanks in advance


Source: (StackOverflow)