Home > Net >  Django form errors appearing without error
Django form errors appearing without error

Time:11-09

I made a research here in Stack and my problem is the opposite of the majority, I saw some ways to make it appear, but my problem is that it's appearing when the user hits the "Register" button / Refresh the register page. So it's an annoying thing that appears wherever the user enter/refresh the page because the form is empty.

View.py

@unauthenticated_user
def register(request):
    form_u = CreateUser(request.POST)
    form_c = CreateClient(request.POST)
    if request.method == 'POST':
        form_u = CreateUser(request.POST)
        form_c = CreateClient(request.POST)
        if form_u.is_valid() and form_c.is_valid():
            user = form_u.save()
            group = Group.objects.get(name='func')
            user.groups.add(group)
            client = form_c.save(commit=False)
            client.user = user
            client.save()
            return redirect('login')
        else:
            form_u = CreateUser()
            form_c = CreateClient()

    context = {'form_u': form_u, 'form_c': form_c}
    return render(request, 'register.html', context)

HTML

<form method="POST" action="" id="ativa">
                    {% csrf_token %}
...

</form>
            

            {{form_u.errors}}
            {{form_c.errors}}

            <div class="mt-4">
                <div class="d-flex justify-content-center links">
                    Have an account ? <a href="{% url 'login' %}" class="ml-2">Login</a>
                </div>
            </div>

Print

Form error

P.S: The site is in portuguese, but I can share the form link in heroku

CodePudding user response:

Your logic is opposite of what you want: Initialize the forms with POST data regardless of whether the request is a POST or a GET request, which will result in the errors if there is no POST data. Then you initialize empty forms when the form data is invalid.

Instead you'll want to pass POST data only if the request is a POST request, and you should initialize empty forms only if the request is not a POST request:

@unauthenticated_user
def register(request):
    # If request is POST, validate forms and add objects.
    if request.method == 'POST':
        form_u = CreateUser(request.POST)
        form_c = CreateClient(request.POST)
        if form_u.is_valid() and form_c.is_valid():
            user = form_u.save()
            group = Group.objects.get(name='func')
            user.groups.add(group)
            client = form_c.save(commit=False)
            client.user = user
            client.save()
            return redirect('login')
         # We can remove the else statement here, 
         # because the function either redirects or resumes 
         # normal flow and renders the template 
         # with the form errors.
    else:
    # Only initialize empty forms when no POST request was made.
        form_u = CreateUser()
        form_c = CreateClient()

    context = {'form_u': form_u, 'form_c': form_c}
    return render(request, 'register.html', context)
  • Related