django form fields code example

Example 1: django ModelChoiceField value not id

articles = ModelChoiceField(queryset=Articles.objects.all(),
        to_field_name='slug')

Example 2: django form field class

subject = forms.CharField(label='subject', max_length=100 , widget=forms.TextInput(attrs={'class': "form-control"}))

Example 3: django form list option

# iterable 
GEEKS_CHOICES =( 
    ("1", "One"), 
    ("2", "Two"), 
    ("3", "Three"), 
    ("4", "Four"), 
    ("5", "Five"), 
) 
  
# creating a form  
class GeeksForm(forms.Form): 
    geeks_field = forms.ChoiceField(choices = GEEKS_CHOICES)

Example 4: django get form id from request

# html
<input type="submit" value="Reply" name ="message_frm">

# views.py
if 'message_frm' in request.POST:
    #do somethings

Tags:

Misc Example