Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:51:36+00:00 2026-06-08T23:51:36+00:00

I am having trouble figuring out how to control field validation for ModelForms. Here

  • 0

I am having trouble figuring out how to control field validation for ModelForms.

Here is my Model Form:

class UserForm(ModelForm):
    class Meta:
        model = User
        fields = ('email', 'password')

Here is how I am rendering my form in the template:

<form method="post" action="">
    {% csrf_token %}
    {{ userform.as_p }}
    <input type="submit" value="Submit" />
</form>

Here is my view:

def login_page(request):
    if request.method == 'POST':
        userform = UserForm(request.POST)
        if userform.is_valid():
            email = request.POST['email']
            password = request.POST['password']
            user = authenticate(username=email, password=password)
            if user is not None:
                login(request, user)
                return redirect('/dashboard/')
            else:
                # How can I give the user feedback about authentication failute here. Right now it just reloads the form without any messages.
                return render(request, "login.html", {"userform": userform}) 
        else:
            # Because .is_valid() is called, The userform will validate for email addresses and that a password input is present here.
            return render(request, "login.html", {"userform": userform}) 
    else:
        userform = UserForm()
        return render(request, "login.html", {"userform": userform})

From reading the docs, this seems like it should be as simple as passing my form some custom attribute; but since I am working with a ModelForm, I am unsure whether this is achieved in the same way.

Could anyone provide an example or tips on how best to control ModelForm field errors?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:51:38+00:00Added an answer on June 8, 2026 at 11:51 pm

    The Django authentication app has a built in AuthenticationForm that you use. If you look at the source code, you can see their approach is to check the username and password inside the clean method. That way, a validation error can be raised if the username and password combination is invalid, and the form will display the error.

    You can import the AuthenticationForm and use it in your login view with the following import:

    from django.contrib.auth.forms import Authentication
    

    You may also be able to use the built in login view as well. This means you can take advantage of the added functionality, such as displaying a message if the user does not have cookies enabled. It also decreases the chance of you creating a security vulnerability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble figuring out databases in VB.NET. (VS 2008) What control(s) do I
im having trouble figuring out how to bind mouseout() to my entire nav bar
I've having trouble figuring out how to select based on a list in a
I'm having trouble figuring out how to filter a queryset appropriately for the following
I'm having trouble figuring out how to get my navigation property to update with
I am having trouble figuring out what the elements are in parsed xml input,
I am having trouble figuring out what is causing the click event to fire
I'm having trouble figuring out how to delete a set of records when a
I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged AutoPostBack
I'm having trouble figuring out how to create a foreign key constraint. My data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.