0

This is my template which I am using in the login page but the problem is fields are not showing up .I want to use mdbootstrap on the page.i have searched it on many websites but did't got a solution and every one was using the same thing to use the form is the something I am missing in my code?

<form action="{% url 'log_in' %}" method="POST">

              {% csrf_token %}

    <div class="md-form">

        <i class="fa fa-envelope prefix"></i>
        {{ form.username }}
        {{ form.username.label_tag }}

     </div>

    <div style="padding:5px"></div>

    <div class="md-form"  >

        <i class="fa fa-lock prefix"></i>
        {{ form.password.label_tag }}
        {{ form.password }}

    </div>
            {% if requset.GET.next %}
            <input type="hidden" name="next" value="{{ request.GET.next }}">
            {% endif %}

    <button type='submit' class="btn info-color ">log in</button>

 </form>

and forms.py

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


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

my view function is

def log_in(request):
    if request.user.is_authenticated:
        return render(request,'registration/userhome.html')
    elif request.POST:
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user.is_active:
            login(request, user)
            return render(request,"registration/userhome.html")
    else :
        return HttpResponse("Invalid login details supplied.")        

views login_view()

def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = LoginForm()
    return render(request,"registration/login.html",{'form':form})

I have not even rendered the login page but still they are working and the part of form feilds are missing in it

my urls.py file

from django.urls import path
from . import views
from django.contrib.auth.views import LoginView


urlpatterns =[

    path("",views.index,name='index'),
    path("userhome/",views.userhome,name='userhome'),
    path("quiz/", views.quiz, name='quiz'),
    path("signup/", views.sign_up,name='sign_up'),
    path("login/", views.login_view,  name='login'),
    path("login/", views.log_in,  name='log_in'),
    path("index", views.log_out,name='log_out'),
    path("rules/",views.rules,name='rules'),

]
Utpal Dutt
  • 383
  • 3
  • 18

1 Answers1

0

You must have a view like this :

def myView(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = LoginForm()
    return render(request, 'myTemplate.html', {'form': form})
Ali
  • 2,541
  • 2
  • 17
  • 31