Search code examples
pythondjangodjango-forms

form validation error message not displaying


There was no displaying error message in website and there was not any elements for view errors

view.py:

from . import forms
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login,logout

from django.core import validators
 

def user_login(request):
    form=forms.LoginForm()
    if request.POST:

     form=forms.LoginForm(request)
    
    # if request.user.is_authenticated:
    #     return redirect('home')
    
    return render(request,"login.html",context={'form':form,'message':''})


def home(request):
    if request.user.is_authenticated:
       return render(request,"home.html")
    return redirect(user_login)

def user_logout(request):
    if request.user.is_authenticated and request.META.get('HTTP_REFERER') is not None:
       logout(request)
    return redirect(home)

forms.py:

from django import forms    
from django.core import validators


class  LoginForm(forms.Form):
    name= forms.CharField(validators=[validators.MinLengthValidator(4,"errorororokjjkkj hjkhjkjhk hkhkh j")],widget=forms.TextInput(attrs = {'class':"form-control mb-3",'placeholder':"Username"}))
    password=forms.CharField(widget=forms.PasswordInput(attrs = {'class':"form-control mb-3",'placeholder':"password"}))

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>login</title>
    {% load bootstrap5 %}
  {% bootstrap_css %}
  {% bootstrap_javascript %}
</head>
<body>
  <div class="  w-50 container " style="border-radius: 20px; background-color: darkgrey;">
  <h1 class="mt-5 pt-5 mb-0 text-center   ">login</h1>
  <div class="h-100 d-flex align-items-center justify-content-center ">

    <form class="w-50 m-5  container" method="post" >
      {%csrf_token%}
      
      {{form}}
      {{message}}
      <button type="submit" class="btn btn-primary mt-1" onclick="">login</button>
    </form>  
    
     
  <!-- <form class="w-50 m-5  container" method="post" enctype="multipart/form-data"   >
      {%csrf_token%}
      <label for="exampleFormControlSelect1">Username</label>
      <input type="text" class="form-control mb-3" placeholder="Username" name="username" required>
      <p>{{message}}</p>
      <label for="password">password</label>
      <input class="form-control mb-3" type="password" name="password" placeholder="password" required></input>
    
      <button type="submit" class="btn btn-primary mt-1" onclick="">login</button>
    </form> -->
  </div>
</div>
</div>
</body>
</html>

I want display the error without using from request.POST values. Please help me for validate it. I also tried custom validator using validationerror method. When I use request.POST method it will displaying error which I passed the message.


Solution

  • you need to check for validation in the view

    def user_login(request):
        if request.method == 'POST':
            form = forms.LoginForm(request.POST)
            if form.is_valid():
                # Handle valid form submission
                
                return redirect('home')
        else:
            form = forms.LoginForm()
    
        return render(request, "login.html", context={'form': form})
    

    also add the error messages in the HTML

    <form class="w-50 m-5 container" method="post">
      {% csrf_token %}
      
      {{ form.name.label_tag }} {{ form.name }}
      <div class="error-message">{{ form.name.errors }}</div>
    
      {{ form.password.label_tag }} {{ form.password }}
      <div class="error-message">{{ form.password.errors }}</div>
    
      <button type="submit" class="btn btn-primary mt-1">Login</button>
    </form>