You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django登录视图中表单错误信息无法显示的问题排查

Fixing Django Form Error Messages Not Showing in Login Template

Hey folks, I recently ran into a frustrating issue while building a personal project with Django: when I submitted an invalid login form, the error messages wouldn't show up in my template. After digging into my view code, I found the root cause and fixed it. Let me share what worked for me.

The Correct Login View Code

Here's the view that finally got error messages displaying properly:

def login_view(request):
    if request.method == 'POST':
        # Initialize form with POST data
        form = LoginForm(request.POST)
        if form.is_valid():
            # Handle successful login
            form.login(request)
            return redirect('/')
        # If form is invalid, we DON'T reinitialize it here!
    else:
        # Empty form for GET requests
        form = LoginForm()
    
    # Pass the form (either empty or with errors) to the template
    context = {'form': form}
    return render(request, 'member/login.html', context)

What Was the Problem?

My initial mistake was re-creating a fresh LoginForm() instance inside the POST branch when the form was invalid. That meant I was passing an empty, error-free form to the template instead of the one that had validation errors attached.

The key fix here is: whether the form is valid or not, after processing the POST request, we keep the same form instance (which now has error messages if validation failed) and pass it to the template.

How to Display Errors in the Template

Make sure your template is set up to render the form's error messages. Here's a simple example:

<form method="post">
    {% csrf_token %}
    
    <!-- Render form fields -->
    <div class="form-group">
        {{ form.username.label_tag }}
        {{ form.username }}
        <!-- Show errors for the username field -->
        {% if form.username.errors %}
            <small class="text-danger">{{ form.username.errors }}</small>
        {% endif %}
    </div>
    
    <div class="form-group">
        {{ form.password.label_tag }}
        {{ form.password }}
        <!-- Show errors for the password field -->
        {% if form.password.errors %}
            <small class="text-danger">{{ form.password.errors }}</small>
        {% endif %}
    </div>
    
    <!-- Show global form errors (e.g., invalid username/password combination) -->
    {% if form.non_field_errors %}
        <div class="alert alert-danger">
            {{ form.non_field_errors }}
        </div>
    {% endif %}
    
    <button type="submit">Log In</button>
</form>

Quick Tips to Avoid This Issue

  • Never reinitialize the form inside the POST branch when validation fails. The form instance created with request.POST already holds all the error data.
  • Always pass the same form instance to the template, regardless of whether the request was GET or POST.
  • Double-check that your template is referencing the correct error attributes (field.errors for individual fields, form.non_field_errors for cross-field errors).

内容的提问来源于stack exchange,提问作者zoozooclub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:58:58