Django Level Three Flashcards
List 3 advantages of using Django’s forms functionality?
- Generate HTML form widgets quickly
- Quicker data validation and processing into a python data structure
- Create form versions of Models, models can be updated from forms.
What are all the necessary tasks (5) for adding a form to your application?
- Create the forms.py file
- Create the forms; similar to models
- Create a view for the form
- Map the view
- Code out the html template
syntax for template tagging?
{{ form }}
what does HTTP stand for?
Hypertext Transfer Protocol
What was HTTP designed for?
communication between a client and a server where the client submits a request and the server responds with what was requested.
What does a GET Request do?
Requests data from a resource
What does a POST request do?
submits data to be processed into a resource
in the template tag {{ form.as_p }} what is the .as_p for?
formats the form to look nicer
what do you need to place under your forms template tag in order for it to work?
{% csrf_token %}
what does csrf_token stand for?
cross site request forgery token
what does the csrf_token do in a nutshell?
verifies data
make sure it gets to where it’s going
How does a csrf_token work?
checks to see that “hidden input” code matches
A form class inherits from …
forms.Form in the same way that a model class inherits from models.Model
in a form class, field classes are accessed from what module?
the forms module
What arg can you add into the CharFIeld() class to format how it looks?
widget=forms.Textarea
when importing from the same directory that the current file is in, what can you use in place of the name of that file?
a dot.
What is the naming convention for a forms view function?
form_name_view
How do you pass a form into a view?
instantiate it:
in the forms template, the form tag should have a method attribute set equal to ___
“POST”
in the template, what goes inside of the form tag
2 template tags containing the form and the csrf token
what must you add to the view function to have it actually do something with the forms information when submitted?
if request.method == ‘POST’
inside of the if request.method == ‘POST’: block there must be …
- a new instance of the form pulled from request.POST
- a validation check on the form with code to run if it passes and or fails.
How do you reference data that was passed into the form?
form.cleaned_data[‘name’]
this grabs whatever info got passed into the field called name in the form.
Steps for adding a botcatcher validator to a form?
- create the field in the form class
- pass in 3 args
- required=False
- widget=forms.HiddenInput
- validators=[validators.MaxLengthValidator(0)]

How do you make and use your own validation check?
- create a function in the forms.py file that takes in a value and checks for a certain condition and raises a validation error if things dont check out.
- pass that function into an attributes field class using a kwarg validators=[function_name]

How do you create validation checks using the clean() function?
- def the clean function within the forms class
- instantiate super().clean() as all_clean_data (arbitrary name)
- instantiate the data from whichever fields usign all_clean_data[‘field’]
- use that data to make a validation check using logic

Where is forms imported from?
django
Where is validators imported from?
django.core