Python | Django Forms

Last Updated :
Discuss
Comments

Question 1

If the template renders {{ form }} for the form below:

Python
class MyForm(forms.Form):
    name = forms.CharField(max_length=100)
    age  = forms.IntegerField()

Which method of rendering is being used by default?

  • form.as_table()

  • form.as_p()

  • form.as_div()

  • form.as_ul()

Question 2

When a form modifies data on the server which HTTP method should be used in Django?

  • GET

  • DELETE

  • POST

  • PUT

Question 3

In a ModelForm with Meta.fields = 'all' what is the main security risk?

  • May skip mandatory fields

  • Converts widgets to TextInput

  • Disables validation

  • May include sensitive fields like is_admin

Question 4

In a template the code below is used to render a form manually. What does form.field_name display?

HTML
{{ form.non_field_errors }}
{{ form.field_name.errors }}
{{ form.field_name }}
  • Only widget

  • Only label

  • Label and widget

  • Nothing

Question 5

In the view below which option is not a valid reason for form.is_valid() to return False?

Python
def my_view(request):
    form = MyForm(request.POST or None)
    if form.is_valid():
        pass
  • Widget HTML failed to render

  • Required field is empty

  • ValidationError raised in clean()

  • Value type conversion failed

Question 6

What is the effect of this code?

Python
modelformset_factory(Book, fields=['title', 'author'], extra=3)
  • Shows only existing Book objects

  • Prevents deletion

  • Limits to exactly 3 forms

  • Adds 3 empty forms for new Books plus existing ones

Question 7

When sending an Ajax POST request to Django which element must be included to avoid a 403 Forbidden error?

  • form ajax="true"

  • method="get"

  • enctype="multipart/form-data"

  • <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

Question 8

Which method safely renders only selected fields in a ModelForm?

  • Use Meta.fields = 'all' and hide unwanted fields with CSS

  • Use Meta.fields = ['required_field1', 'required_field2']

  • Modify init to remove unwanted fields

  • Use Meta.exclude = ['field_x', 'field_y']

Question 9

In a ModelFormSet how can the total number of forms shown be limited to 5?

  • extra=5

  • queryset=Model.objects.none(), extra=5, max_num=5

  • extra=0, max_num=5

  • max_num=5

Question 10

Which built-in method renders a form as an unordered list with <li> tags?

  • form.as_table()

  • form.as_div()

  • form.as_ul()

  • form.as_p()

Tags:

There are 16 questions to complete.

Take a part in the ongoing discussion