Questions tagged [django-forms]

Specific questions related to forms with the Django web framework

Specific questions related to Django forms.

Quote from the official documentation:

While it is possible to process form submissions just using Django’s HttpRequest class, using the form library takes care of a number of common form-related tasks. Using it, you can:

  • Display an HTML form with automatically generated form widgets.
  • Check submitted data against a set of validation rules.
  • Redisplay a form in the case of validation errors.
  • Convert submitted form data to the relevant Python data types.

An example form from the docs

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)

An example on how to validate a form with POST data

form = ContactForm(request.POST)  # A form bound to the POST data
if form.is_valid():
    return HttpResponseRedirect('/thanks/')
17321 questions
262
votes
7 answers

Django set default form values

I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile) ts = models.IntegerField(max_length=15) title = models.CharField(max_length=50) body =…
Mike
  • 6,385
  • 8
  • 51
  • 76
243
votes
8 answers

How do I filter ForeignKey choices in a Django ModelForm?

Say I have the following in my models.py: class Company(models.Model): name = ... class Rate(models.Model): company = models.ForeignKey(Company) name = ... class Client(models.Model): name = ... company = models.ForeignKey(Company) …
Tom
  • 36,698
  • 31
  • 90
  • 98
210
votes
9 answers

How do I add a placeholder on a CharField in Django?

Take this very simple form for example: class SearchForm(Form): q = forms.CharField(label='search') This gets rendered in the template: However, I want to add the placeholder attribute to this field…
Joelbitar
  • 3,170
  • 4
  • 24
  • 28
199
votes
13 answers

Define css class in django Forms

Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker = forms.BooleanField(required=False) Is there a way for me to define css classes on each field such that…
ashchristopher
  • 21,525
  • 16
  • 44
  • 49
188
votes
2 answers

Create empty queryset by default in django form fields

I have this fields in form: city = forms.ModelChoiceField(label="city", queryset=MyCity.objects.all()) district = forms.ModelChoiceField(label="district", queryset=MyDistrict.objects.all()) area = forms.ModelChoiceField(label="area",…
TheNone
  • 5,078
  • 10
  • 46
  • 95
169
votes
5 answers

How can I build multiple submit buttons django form?

I have form with one input for email and two submit buttons to subscribe and unsubscribe from newsletter:
{{ form_newsletter }}
veena
  • 1,733
  • 2
  • 11
  • 6
165
votes
17 answers

CSS styling in Django forms

I would like to style the following: forms.py: from django import forms class ContactForm(forms.Form): subject = forms.CharField(max_length=100) email = forms.EmailField(required=False) message =…
David542
  • 96,524
  • 132
  • 375
  • 637
159
votes
8 answers

What's the best way to store Phone number in Django models

I am storing a phone number in model like this: phone_number = models.CharField(max_length=12) User would enter a phone number and I would use the phone number for SMS Authentication This application would be used globally. So I would also need…
pynovice
  • 6,124
  • 20
  • 63
  • 100
155
votes
12 answers

Django Passing Custom Form Parameters to Formset

This was fixed in Django 1.9 with form_kwargs. I have a Django Form that looks like this: class ServiceForm(forms.Form): option = forms.ModelChoiceField(queryset=ServiceOption.objects.none()) rate =…
Paolo Bergantino
  • 449,396
  • 76
  • 509
  • 431
147
votes
7 answers

Change a Django form field to a hidden field

I have a Django form with a RegexField, which is very similar to a normal text input field. In my view, under certain conditions I want to hide it from the user, and trying to keep the form as similar as possible. What's the best way to turn this…
Rory
  • 48,706
  • 67
  • 174
  • 234
143
votes
8 answers

Creating a dynamic choice field

I'm having some trouble trying to understand how to create a dynamic choice field in django. I have a model set up something like: class rider(models.Model): user = models.ForeignKey(User) waypoint = models.ManyToManyField(Waypoint) class…
whatWhat
  • 3,589
  • 6
  • 33
  • 42
134
votes
8 answers

Django Forms: if not valid, show form with error message

In Django forms, it can check whether the form is valid: if form.is_valid(): return HttpResponseRedirect('/thanks/') But I'm missing what to do if it isn't valid? How do I return the form with the error messages? I'm not seeing the "else" in…
user984003
  • 23,717
  • 51
  • 158
  • 250
133
votes
8 answers

Django templates: verbose version of a choice

I have a model: from django.db import models CHOICES = ( ('s', 'Glorious spam'), ('e', 'Fabulous eggs'), ) class MealOrder(models.Model): meal = models.CharField(max_length=8, choices=CHOICES) I have a form: from django.forms import…
Artur Gajowy
  • 1,813
  • 2
  • 16
  • 16
120
votes
7 answers

Django: multiple models in one template using forms

I'm building a support ticket tracking app and have a few models I'd like to create from one page. Tickets belong to a Customer via a ForeignKey. Notes belong to Tickets via a ForeignKey as well. I'd like to have the option of selecting a Customer…
neoice
  • 2,103
  • 3
  • 18
  • 15
116
votes
7 answers

What is the equivalent of "none" in django templates?

I want to see if a field/variable is none within a Django template. What is the correct syntax for that? This is what I currently have: {% if profile.user.first_name is null %}

--

{% elif %} {{ profile.user.first_name }} {{…
goelv
  • 2,384
  • 8
  • 26
  • 38
1
2 3
99 100