Home > Software design >  POST HTML FORM verificaton doesn't work with Django
POST HTML FORM verificaton doesn't work with Django

Time:04-30

I am currently learning Django,I connected my register FORM with my views.py, and wrote a little backend code, the problem, is that it links successfully with my .html file, and the POST elements are registering, but as I try to make a verification, (if POST_element < 10), it does nothing.

Edit: The Post elements are recognised by my backend, I test it out with a print(username) statement and it works fine.

Here is a part of my HTML Register FORM:

<div >
        <div  style="background:black;">
            <div >
                <form action="{% url 'register' %}" method="POST" >
                    {% csrf_token %}
                    <span >
                        <i ></i>
                    </span>
                    <span >
                        Register
                    </span>

                    <div  data-validate = "Enter username">
                        <input  type="text" name="username" placeholder="Username">
                        <span  data-placeholder="&#xf207;"></span>
                    </div>

Here is a part of my views.py that manages register:

def register(request):
    if request.method=="POST":
        username = request.POST["username"]
        password = request.POST["pass"]
        password_confirm = request.POST["pass-confirm"]
        email = request.POST["mail"]
    
        if len(username) < 7:
            messages.error(request,"Username must be more than 10 char.") #Don't get any error

        else:
            messages.success(request,"Success")

    return render(request, 'users/register.html')

Here is my urls.py:

urlpatterns = [
    path('', views.register, name='register'),
    path('login/', views.login, name='login')
]

CodePudding user response:

An Excerpt from the django-doc about POST request.

You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.

You haven't return any response in POST request, and the messages framework whether any messages consists of error,success,etc. will send messages to next redirected page, so that's why you are not able to see the message, and your condition is working.

check the below code:

Html code:

<div >
    <div  style="background:black;">
        <div >
            <form   method='POST' action="{% url 'users:register' %}" >
                {% csrf_token %}
                <span >
                    <i ></i>
                </span>

                <span >
                    Register
                </span>

                <div  data-validate = "Enter username">
                    <input  type="text" name="username" placeholder="Username" required>
                    <span  data-placeholder="&#xf207;"></span>
                </div>

                <div  data-validate="Enter password">
                    <input  type="password" name="pass" placeholder="Password" required>
                    <span  data-placeholder="&#xf191;"></span>
                </div>

                <div  data-validate="Confirm password">
                    <input  type="password" name="pass-confirm" placeholder="Confirm Password" required>
                    <span  data-placeholder="&#xf191;"></span>
                </div>

                <div  data-validate="Enter Email">
                    <input  type="email" name="mail" placeholder="E-Mail" required>
                    <span  data-placeholder="&#xf191;"></span>
                </div>

                <div >
                    <button >
                        Register
                    </button>
                </div>

                <div >
                    <a  href="login">
                        Already registered?
                    </a>
                </div>

            </form>
        </div>
    </div>
</div>

Set following style at the top of page in <style> tag, i.e. inline style:

<style>
    .green{
        color:green;
        font-size:1.3rem;
    }
    .red{
        color:red;
        font-size:1.3rem;
    }
</style>

urls.py

app_name='users'
urlpatterns = [
    path('', views.register, name='register'),
    path('login/', views.login, name='login')
]

Note: Giving app_name is a good practice,works like template namespacing while giving urls.

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.contrib import messages
from django.urls import reverse


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        password = request.POST["pass"]
        password_confirm = request.POST["pass-confirm"]
        email = request.POST["mail"]

        # Here everying exist.
        print("UserName : ", username)
        print('Email : ', email)
        print('Password : ', password)
        print('Password Confirm : ', password_confirm)

        if len(username) < 7:
            # Here error exist.
            print('Username must be more than 10 char.')
            messages.error(
                request, "Username must be more than 10 char.", 'red')
            return HttpResponseRedirect(reverse('users:register'))

        else:
            messages.success(request, "Success! form submitted.", 'green')
            return HttpResponseRedirect(reverse('users:register'))

    return render(request, 'users/register.html')

Note: You can also do password hashing through make_password which is from django.contrib.auth.hashers import make_password.

  • Related