Any idea on why my django LoginView form isn't being displayed?

Denzel Hooke

I'm trying to display the LoginView form but it won't display. I've been looking at the documentation for almost an hour and I cannot seem to figure out why the form is not being displayed. It accepts my template so it's rendering the template properly but not creating the form within the HTML form tags.

urls.py

from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from users import views as user_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register, name='register'),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
    path('', include('blog.urls')),
]

Login HTML

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <!-- fieldset used to group related elements into a form -->
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Log In</legend>
                <!-- Loads in the form_key's value within our view with P tags instead-->
                <!-- {{ form_key }} -->
                
                {{ form_key|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Sign Up</button>
            </div>
        </form>
        <div class="border-top pt-3">
            <small class="text-muted">
                Don't have an account yet? <a class="ml-s" href="{% url 'register' %}">Sign up now</a>
            </small>
        </div>
    </div>
{% endblock content %}

BASE HTML

{% load static %}
<!-- Loads in our static folder -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <!-- 'static' below, generates an absolute URL to the static files and accesses the blog/main.css -->
    <link rel="stylesheet" type="text/css" href="{% static 'blog/main.css' %}">
    
    <!-- If there's a key named 'title' passed to the render func. Display that title key's value as 'Django Blog - title' -->
    {% if title %}
        <title>Django Blog - {{ title }}</title>
    {% else %}
        <title>Django Blog</title>
    {% endif %}
</head>
<body> 
    <header class="site-header">
        <nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
          <div class
          ="container">
            <a class="navbar-brand mr-4" href="{% url 'blog-home' %}">Django Blog</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarToggle">
              <div class="navbar-nav mr-auto">
                <a class="nav-item nav-link" href="{% url 'blog-home' %}">Home</a>
                <a class="nav-item nav-link" href="{% url 'blog-about' %}">About</a>
              </div>
              <!-- Navbar Right Side -->
              <div class="navbar-nav">
                <a class="nav-item nav-link" href="{% url 'login' %}">Login</a>
                <a class="nav-item nav-link" href="{% url 'register' %}">Register</a>
              </div>
            </div>
          </div>
        </nav>
    </header>
    <main role="main" class="container">
        <div class="row">
          <div class="col-md-8">
            {% if messages %}
              {% for message in messages %}
              <!-- django's alert message methods have the same names as bootstraps alart message names.
              So we grab the tag anme of that message by using the message.tags which will correspond with the bootstrap alert.  alaert-success message.success -->
                <div class="alert alert-{{ message.tags }}">
                  {{ message }}
                </div>
              {% endfor %}
            {% endif%}
            {% block content %}{% endblock %}
          </div>
          <div class="col-md-4">
            <div class="content-section">
              <h3>Our Sidebar</h3>
              <p class='text-muted'>You can put any information here you'd like.
                <ul class="list-group">
                  <li class="list-group-item list-group-item-light">Latest Posts</li>
                  <li class="list-group-item list-group-item-light">Announcements</li>
                  <li class="list-group-item list-group-item-light">Calendars</li>
                  <li class="list-group-item list-group-item-light">etc</li>
                </ul>
              </p>
            </div>
          </div>
        </div>
    </main>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
markwalker_

I'm fairly sure the standard LoginView uses FormMixin which will add form to the context. So change form_key to form in your template.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related