Django: Where generic forms like LoginView renders functions dictionary fill and how can I change it

347 Views Asked by At

I use LoginView class in Django. We can use this class as login view with a HTML template, like below:
urls:

from django.contrib.auth.views import LoginView

urlpatterns = [
path('', LoginView.as_view(), {'template_name': 'login.html'}),
]

login.html:

<head>
   <title>test<title>
   {% load static %}
</head>
<body>
   <form method="POST">
      {% csrf_token %}
      {{ form.as_p }}
   </form>
</body>

my question is where renders function dictionary or httprequest fill? and how can I change, form.as_p to login_form.as_p, for example?

2

There are 2 best solutions below

1
shafikshaon On

You can get help from Django official doc

2
Andrey Leontyev On

You must use custom login view like this:

from django.contrib.auth.views import LoginView

class CustomLoginView(LoginView):

  def get_context_data(self, **kwargs):
    """Insert the form into the context dict."""
    if 'login_form' not in kwargs:
        kwargs['login_form'] = self.get_form()
    # Another your context
    return super().get_context_data(**kwargs)