In Dajngo, does the UserCreationForm or its parent classes handle the POST and GET logic? Or how is that handled?

I am new with django and going over some code that I don’t understand. I looked through the django source code trying to find out if the class UserCreationFrom (pasted below) from Django handles the logic of POST for you, or if this is implemented elsewhere.

To clarify, I have been seeing a class used for rendering the form then some type of view validating the data. It typically goes like:

Form Class:

class UserForm(forms.ModelForm):
     class Meta:
         model = User
         fields = ("first_name", "last_name")


def update_profile(request):
    if request.method == "POST":
        user_form = UserForm(request.POST, instance=request.user)
    if user_form.is_valid():
        return redirect("somewhere")
        user_form = UserForm(instance=request.user)
        return render(request, "profile.html", {"user_form":user_form})

That implementation makes sense.

What I don’t get is when there is a view that renders the form but then the form itself handles validation and leaves out the if request.method == "POST" logic as shown above. Or is this in some parent class? Is either one of these inline with django convention as well?

An example:

Class View:

class UserSignupView(CreateView):
    model = CustomUser
    form_class = UserProfileSignupForm
    template_name = 'signup_form.html'

    def get_context_data(self, **kwargs):
        kwargs['is_confused'] = True
        return super().get_context_data(**kwargs)
    def form_valid(self, form):
        user =
        login(self.request, user)
        return redirect('somewhere')

Then the form with its logic would be like:

class UserSignUpForm(UserCreationForm):
   class Meta(UserCreationForm.Meta):
       model = User

   def save(self):
         user = super().save(commit=False)
         user.is_confused = True
         return user

Any advice and clarification is greatly appreciated.

Like I said, I’ve looked through the django docs and cannot figure this out. I think I’m missing something basic or if the code I have doesn’t present the full story here. To clarify the following forms are to the same thing though they are not for an identical purpose. I got one from one model/app and the other from another. I assume either could be modified slightly and used wherever this functionality is needed.

>Solution :

In general, Django Form does not handle GET vs POST logic, that’s usually handled by the view.

The user forms are handled by the login/logout view in the auth views.

You can use:

urlpatterns = [
    path("accounts/", include("django.contrib.auth.urls")),

to include all the authentication view in their standard locations, or you can add the individual views into your (e.g. LoginView and friends) for more fine grained control.

These class-based views inherits from the generic form handling class based view and/or generic editing views.

Ultimately, the way class based view works is that they’re really just regular function based views under the hood, but they were implemented as a class which provides some common logic and some hook functions that you can override to customise the generic behaviour.

Leave a Reply