A
A
Artur Kudashev2019-01-13 17:20:50
Django
Artur Kudashev, 2019-01-13 17:20:50

How to create a custom form?

Good day. I'm learning Django and got to forms. I did not fully understand how to create my own forms with css styles. Most examples show how to make simple forms without css styles. Can you give an example of a form with styles? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-01-13
@archi_kud

The first option is elementary:

.some-class input[type="text"] {
    width: 100%;
}
.some-class input[name="some_input"] {
    border: 1px solid red;
}

<form class="some-class">
    {{ form }}
</form>

Option two - with setting classes for individual fields:
class SomeForm(forms.Form):
    some_input = forms.CharField(label='Какое-то поле', widget=forms.TextInput(attrs={'class': 'some-class'}))

What you could read about in the documentation .

D
Dimonchik, 2019-01-13
@dimonchik2013

https://github.com/jazzband/django-widget-tweaks
but there are other ways

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question