0

I am trying something that I believe must be so simple but nothing has worked.

I have added the auth by adding into my urls.py:

path('accounts/', include('django.contrib.auth.urls'), {'template_name': 'login.html', 'authentication_form': forms.LoginForm}),

This is my urls.py:

from django.contrib import admin
from django.urls import path, include
from reports import views, forms

urlpatterns = [
    path('', views.IndexView.as_view(), name='home'),
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls'), {'template_name': 'login.html', 'authentication_form': forms.LoginForm}),

]

My project has an app called reports, in here i have my templates folder and i have added my registration folder with login.html and all the relevant files.

I have also created in my project (reports) a file forms.py and added

from django.contrib.auth.forms import AuthenticationForm
# from django import forms

class LoginForm(AuthenticationForm):
    username = forms.CharField(widget=forms.TextInput(
        attrs={
        'class':'form-control',
        'placeholder':'Username'
        }
    ))
    password = forms.CharField(widget=forms.PasswordInput(
        attrs={
        'class':'form-control',
        'placeholder':'Password'
        }
    ))

My form is displayed but the class and placeholder is not been added

any suggestion of what i need to add or change?

my current login.html

<form class="form-signin" method="post">
  {% csrf_token %} 
  {{ form.as_p }}
  <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  <p class="mt-5 mb-3 text-muted">
    <a href="{% url 'password_reset' %}">Lost password?</a>
  </p>
</form>
Manza
  • 2,109
  • 1
  • 27
  • 34

2 Answers2

0

You can't set parameters on an include like that, that's not how it works. You will need to define the login view separately. Because of the way Django's URL resolution works, as long as you define the more specific URL first, it will be matched over the later more general one. So:

from django.contrib.auth import views as auth_views

path('accounts/login', auth_views.LoginView.as_view(),  {'template_name': 'login.html', 'authentication_form': forms.LoginForm}, name='login'),
path('accounts/', include('django.contrib.auth.urls'), {'template_name': 'login.html', 'authentication_form': forms.LoginForm}),
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you, I have added those 2 paths and the import, into the urls.py but no effect on the page – Manza Jul 28 '18 at 10:16
-1

Update: Looking at the source code of AuthenticationForm, username and password are class variables. They would probably set to their default values during initialization. You can use django-widget-tweaks. Then you can render those 2 fields:

{% load widget_tweaks %}
<form class="form-signin form-group" method="post">
    {% csrf_token %} 
    <label>{{ form.username.label_tag }}</label>
    {% render_field form.username class+='form-control' placeholder='Username' %}
    <label>{{ form.password.label_tag }}</label>
    {% render_field form.password class+='form-control' placeholder='Password' %}
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

https://github.com/jazzband/django-widget-tweaks

tanjibpa
  • 129
  • 10