1

I have the CRUD views, one of them looks like this:

#model
class Model(models.Model):
    field1 = models.CharField()
    field2 = models.CharField()



#view
class Model1CreateView(CreateView):
    model = Model1
    fields = ['field1', 'field2']

#template
and model1_form.html

    <form action="{% url 'model1_new' %}" method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="Submit" />
    </form>

I need to specify a css class for field1, how can I do that?

3
  • Not a direct answer but github.com/kmike/django-widget-tweaks is useful. Also fields have IDs, sometimes you might want to use one for CSS. Commented Nov 3, 2014 at 11:49
  • @Kos, for such a simple thing I'll have to install all that? No, thanks. Commented Nov 3, 2014 at 11:51
  • 2
    That's not what I meant. The lib is useful because Django normally encourages setting field classes in the Widget instances for a given Form, while it's (arguably) more logical to put that in templates. Commented Nov 3, 2014 at 11:55

2 Answers 2

2

You need to create a Form and modify the attrs of that fields widget. There are two common ways of doing this. In either case, you need to create a custom Form and tell your View to use that form:

forms.py

from django import forms
from myapp.models import FooModel

class FooForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        self.fields['field1'].widget.attrs = { 'class': 'fooclass' }

    class Meta:
        model = FooModel
        fields = ['field1', 'field2']

or

from django import forms
from myapp.models import FooModel

class FooForm(forms.ModelForm):
    field1 = forms.CharField(widget=forms.Textarea(attrs={'class':'fooclass';}))

    class Meta:
        model = FooModel
        fields = ['field1', 'field2']

then in your views.py:

from myapp.forms import FooForm

class Model1CreateView(CreateView):
    model = Model1
    fields = ['field1', 'field2']
    form_class = FooForm
Sign up to request clarification or add additional context in comments.

2 Comments

in Model1CreateView, does it have to be form_class= FooForm or form= FooForm?
form_class. You can see all of the attribute and methods that a CreatView provides here: ccbv.co.uk/projects/Django/1.5/django.views.generic.edit/…. Also, I forgot to add your second field to the form. I've updated
1

You have to define a form explicitly if you want to do this.

class Model1Form(forms.ModelForm):
    field1 = forms.CharField(widget=forms.TextInput(attrs={'class': 'myclass'}))
    class Meta:
        model = Model1
        fields = ['field1', 'field2']

class Model1CreateView(CreateView):
    form_class = Model1Form
    model = Model1

4 Comments

shouldn't I specify field2 in Model1Form as well?
If you're not changing anything from the default (as defined in the model), then no there's no reason to.
also it throws an error: Model1CreateView is missing a queryset. Do you have any idea why?
Apologies, should be form_class and you still need model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.