Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django What is the best way to format forms?

I have just gotten into Django and read a bunch of books, now im building my first app. When I render my forms in my template im using...

{{ form.as_ul }}

And now I'm trying to apply some CSS to my form. It's proving difficult to get it look nice. I was wondering if there is a better way of rendering and styling forms?

like image 310
Ryan Currah Avatar asked Sep 11 '13 13:09

Ryan Currah


People also ask

How can I style Django forms?

Right now this form is using the default widgets, and it doesn't have any style, so, basically, it looks like this: So, to change it, we need to customize the appearance. You can customize widgets in two ways — it can be via widget instance or widget class. For this first example, I'll be using widget instance.

Why use Django crispy forms?

Django-crispy-forms is an application that helps to manage Django forms. It allows adjusting forms' properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.

Should I use Django forms or not?

The question really is "do you have any good reason not to use django. forms?" Because really, if you are not using django. forms you throw away a lot of django's built-in security measures. Even within django itself (for instance the admin site) forms are used heavily.

How to create a form in Django?

Django offers a classified set of methods for formulating the form entities. How to Create a Django Form? Create a Django form are explain below: 1. Create a forms.py file in the application The forms.py file is similar to models.py, All fields used in the form will be declared here under a form class. 2. Create a View for The Form

What does Django mean?

A Django view method is created for the form in the views.py file. An object for the form class is created here. this object is used as a value for the context dictionary in the template rendering.

How to render a form field into a view in Django?

Django form fields have several built-in methods to ease the work of the developer but sometimes one needs to implement things manually for customizing User Interface (UI). A form comes with 3 in-built methods that can be used to render Django form fields. To render this form into a view, move to views.py and create a home_view as below.

Do you have to use widgets in Django?

Spoiler: You have to use widgets. Everyone who uses Django knows how good the Django forms can be. But when you are using it for the first time, a question araises: How can I style it?


1 Answers

Well, there are a couple of options that you can take advantage of like:

  1. form.as_p
  2. form.as_ul
  3. form.as_table

Its up to you to decide what you would like. As for customizing it with CSS, take a look at how they structured the form here (taken from django docs):

<form action="/contact/" method="post">
    {{ form.non_field_errors }}
    <div class="fieldWrapper">
        {{ form.subject.errors }}
        <label for="id_subject">Email subject:</label>
        {{ form.subject }}
    </div>
    <div class="fieldWrapper">
        {{ form.message.errors }}
        <label for="id_message">Your message:</label>
        {{ form.message }}
    </div>
    <div class="fieldWrapper">
        {{ form.sender.errors }}
        <label for="id_sender">Your email address:</label>
        {{ form.sender }}
    </div>
    <div class="fieldWrapper">
        {{ form.cc_myself.errors }}
        <label for="id_cc_myself">CC yourself?</label>
        {{ form.cc_myself }}
    </div>
    <p><input type="submit" value="Send message" /></p>
</form>

The original form looked like this:

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

So, as you can see, you can just access the form element, by

{{ form.<element_name>.<element_properties> }}

You can add your own CSS to the labels or divs. Its totally up to you to decide what you want to do. On a side note, if you want forms formatted with Bootstrap 3, then might I suggest that you use django-crispyforms extension for django.

like image 87
Games Brainiac Avatar answered Sep 23 '22 06:09

Games Brainiac