-1

I am new to python & Django. I am getting one error and have absolutely no idea how to solve it. Any help will be appreciated. enter image description here

from django.shortcuts import render
# Create your views here.
#log/views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
# Create your views here.
# this login required decorator is to not allow to any
# view without authenticating
@login_required(login_url="login/")
def home(request):
     return render(request,"home.html")

The code in urls.py is,

from django.conf.urls import include,url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from log.forms import LoginForm
urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'', include('log.urls')),
 url(r'^login/$', auth_views.login ,{'template_name':   'login.html','authentication_form': LoginForm}),
 url(r'^logout/$', auth_views.logout, {'next_page': '/login'}),
 ]

The code in login.html is,

{% extends 'base.html' %}


{% block content %}
    {% if form.errors %}

<p>Your username and password didn't match. Please try again.</p>
{% endif %}

{% if next %}
    {% if user.is_authenticated %}

<p>Your account doesn't have access to this page. To proceed,
    please login with an account that has access.</p>
    {% else %}

<p>Please login to see this page.</p>
    {% endif %}
{% endif %}

<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Please Sign In</h3>
                </div>
                <div class="panel-body">
                    <form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}

                        <p class="bs-component">
                            <table>
                                <tr>
                                    <td>{{ form.username.label_tag }}</td>
                                    <td>{{ form.username }}</td>
                                </tr>
                                <tr>
                                    <td>{{ form.password.label_tag }}</td>
                                    <td>{{ form.password }}</td>
                                </tr>
                            </table>
                        </p>
                        <p class="bs-component">
                            <center>
                                <input class="btn btn-success btn-sm" type="submit" value="login" />
                            </center>
                        </p>
                        <input type="hidden" name="next" value="{{ next }}" />
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block javascript %}


<script>
{% if not user.is_authenticated %}
$("ul.nav.navbar-nav.navbar-right").css("display","none");
{% endif %}
</script>

{% endblock %}

Hope this much info will do....

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
Vineet Kulkarni
  • 71
  • 1
  • 1
  • 4

1 Answers1

2

Add a name to the login url pattern:

kwargs = {'template_name': 'login.html','authentication_form': LoginForm}
...
url(r'^login/$', auth_views.login, kwargs=kwargs, name='login'),
#                                                  ^^^^

and then use that name in your template:

<form method="post" action="{% url 'login' %}">
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139