[Fixed]-Writing an example django form like class

1👍

Django’s docs contains a lot of information regarding forms, start here.

For example:

from django import forms

# your form:
class UserForm(forms.Form):
    username = forms.CharField(max_length=100)
    email = forms.EmailField(null=True, blank=True)

# and your view:
def user_view(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = Userorm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = UserForm()

    return render(request, 'user.html', {'form': form})

See the link above for more info.

👤Udi

Leave a comment