Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

374
Views
How to raise django validation error on login form (which is on a bootstrap modal on index.html)?
#below is login view
def login_view(request):
        print(request.user.is_authenticated())
        next = request.GET.get('next')
        title = "Login"
        form = UserLoginForm(request.POST or None)
        if form.is_valid():    
            username = form.cleaned_data.get("username")
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            login(request, user)        
            if next:
                return redirect(next)
            return redirect("/userdashboard")
        return render(request, "index.html", {"form":form, "title": title})

how can i return error message on login form (which is in bootstrap modal)

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use a Http404 condition to render error message on login. Import Http404 the class is django.http.Http404 if you raise any error in that case Django will return the error message or page that you set to be displayed ex: 404.html. You code may look like this:

def login_view(request):
    print(request.user.is_authenticated())
    next = request.GET.get('next')
    title = "Login"
    form = UserLoginForm(request.POST or None)
    try:
        if form.is_valid():    
            username = form.cleaned_data.get("username")
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            login(request, user)        
            if next:
                return redirect(next)
            return redirect("/userdashboard")
    except:
        raise Http404 (" This user does not exist.")
    return render(request, "index.html", {"form":form, "title": title})
about 4 years ago · Santiago Trujillo Report

0

inside the bootstrap modal use form errors in loop.

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <div class="alert alert-danger">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        <div class="alert alert-danger">
            <strong>{{ error|escape }}</strong>
        </div>
    {% endfor %}
{% endif %}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!