Home > Mobile >  How can I create a single user form page with django 1.11?
How can I create a single user form page with django 1.11?

Time:06-02

I'm trying to create a single user login page. How can I create that user? Can I just validate username and password in views ? Thank you for your help..

CodePudding user response:

What exactly you want . Kindly elaborate your question . Single user login page means you will register user from that page and also login from that page only ?

If so . Just create a register form , create a save method in it , and then log the user in with that save function .

Or what else you are asking for . Please Elaborate ..

CodePudding user response:

if i understand what you are talking about, You want simple user registration form and login form:

you can use this one:

def login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = auth.authenticate(username=username, password=password)

        if user is not None:
            auth.login(request, user)
            return redirect(#home page url)
        else:
            messages.info(request, 'Invalid Credential') 
            return redirect('login')
    else:        
        return render(request, 'login.html')

def register(request):
    if request.method == 'POST':
        username = request.POST['username']
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password2']

        if password == password2:
           if User.objects.filter(email=email).exists():
                messages.info(request, 'Email or user name Already taking')
                return redirect('register')
           elif User.objects.filter(username=username).exists():
                messages.info(request, 'username is taken')
                return redirect('register')
           else:
                user = User.objects.create_user(username=username, 
            first_name=first_name, last_name=last_name, email=email, 
            password=password)
            user.save();
            return redirect('login')
        else:
            messages.info(request, 'Password Not Match')
            return redirect('register')   
        return redirect ('/')     
    else:
        return render(request, 'signup.html')

add this to your login templates I'm using Bootstrap version 5:

<form method="POST">
                {% csrf_token %}
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Enter Username</label>
                    <input type="text"  name="username">
                </div>
                <br>
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Enter Password</label>
                    <input type="password"  name="password">
                </div>
                <br>
                <a href="{% url 'Rules' %}" style="text-decoration: none; font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">
                    Create An Account
                </a>
                <br>
                <button type="submit" >Login</button>
            </form>

add this into your register page template:

<form action="{% url 'register' %}" method="POST">
                {% csrf_token %}
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Username</label>
                    <input type="text"  name="username">
                </div>

                <br>
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">First Name</label>
                    <input type="text"  name="first_name">
                </div>

                <br>
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Last Name</label>
                    <input type="text"  name="last_name">
                </div>
                
                <br>
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Enter Your Email</label>
                    <input type="email"  name="email">
                </div>
                
                <br>
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Password</label>
                    <input type="password"  name="password">
                </div>

                <br>
                <div >
                    <label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Repeat Password</label>
                    <input type="password"  name="password2">
                </div>

                <br>
                <a href="{% url 'login' %}" style="text-decoration: none; font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Already Have An Account</a>
                <br>
                <button type="submit" >Create</button>
            </form>
  • Related