Performance: Login view and template
This affects the initial page load speed and interaction responsiveness when users access the login page.
Jump into concepts and practice - no test required
from django.shortcuts import render from django.contrib.auth.forms import AuthenticationForm def login_view(request): form = AuthenticationForm(request) # lightweight built-in form return render(request, 'login.html', {'form': form}) <!-- login.html --> <html lang="en"> <head> <title>Login</title> <link rel="stylesheet" href="/static/css/login-min.css" media="print" onload="this.media='all'"> <noscript><link rel="stylesheet" href="/static/css/login-min.css"></noscript> </head> <body> <main> <form method="post" aria-label="Login form"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> </form> </main> </body> </html>
from django.shortcuts import render def login_view(request): return render(request, 'login.html', {'form': HeavyLoginForm()}) <!-- login.html --> <html> <head> <title>Login</title> <link rel="stylesheet" href="/static/css/large-style.css"> <script src="/static/js/large-script.js"></script> </head> <body> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> </form> <img src="/static/images/large-background.jpg" alt="Background"> </body> </html>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy login form with large CSS/JS and images | High (many nodes, deep tree) | Multiple reflows triggered by images and scripts | High paint cost due to large images | [X] Bad |
| Lightweight login form with deferred CSS and minimal assets | Low (simple form markup) | Single reflow on initial load | Low paint cost, minimal images | [OK] Good |
LoginView?LoginView as a built-in class-based view to handle user login functionality easily.LoginViewLoginView?template_name to specify the HTML template file.template_name is the correct attribute to set the template.<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>{% csrf_token %} tag is removed?{% csrf_token %} tag is missing, Django will reject the POST request with a CSRF verification error.path('login/', LoginView.as_view(template_name='login.html'))LoginView?LoginView to use it.form_valid method to set session expiry longer if 'remember_me' is checked.LoginView to add a custom form with a 'remember_me' field and adjust session expiry in form_valid -> Option A