Home > front end >  name 'user' is not defined
name 'user' is not defined

Time:11-11

I'm trying to make a custom registration page without using usercreationform i've made the model and imported it but when i run it I keep encountering this error NameError at /register name 'user' is not defined the line in particular is:
if user.password != user.repassword:

and i tried changing the method to GET instead of POST and i got a different error MultiValueDictKeyError 'fname'

views.py

from django.shortcuts import render,redirect
from .models import User
from django.contrib import messages




def home(request):
    return render(request, 'home.html')


def register(request):
    global user
    if request.method == 'POST':
        user = User()
        user.fname = request.POST['fname']
        user.lname = request.POST['lname']
        user.email = request.POST['email']
        user.password = request.POST['password']
        user.repassword = request.POST['repassword']

    if user.password != user.repassword:
        return redirect("register")
    elif user.fname == "" or user.password == "" :
        messages.info(request,f"some fields are empty")
        return redirect("register")
    else:
        user.save()
    return render(request, 'register.html')

models.py

from django.db import models


class User(models.Model):
    id=models.AutoField(primary_key=True)
    fname=models.CharField(max_length=100)
    lname=models.CharField(max_length=100)
    email=models.CharField(max_length=50)
    password=models.CharField(max_length=100)
    repassword=models.CharField(max_length=100)

html page

{%extends 'base.html'%}
{%load static%}

{%block content%}
<link rel="stylesheet" href="{%static 'css/style.css'%}">
<form method="post">
    {%csrf_token%}
    <label for="fname">First Name</label>
    <input type="text" name="fname" placeholder="Enter your First Name"><br>
    <label for="lname">Last Name</label>
    <input type="text" name="lname" placeholder="Enter your Last Name"><br>
    <label for="email">Email</label>
    <input type="email" name="email" placeholder="Enter your Email"><br>
    <label for="password">Password</label>
    <input type="password" name="password" placeholder="Enter a password"><br>
    <label for="repassword">Re Password</label>
    <input type="password" name="repassword" placeholder="Re enter your password"><br>
    <button class="btn">Submit</button>
</form>
{%endblock%}

CodePudding user response:

Is this what you want?:

def register(request):
    user = request.user
    if request.method == 'POST':
        user = User()
        user.fname = request.POST['fname']
        user.lname = request.POST['lname']
        user.email = request.POST['email']
    ....
    ....

CodePudding user response:

You should do something if the request method is not POST:

from django.core.exceptions import BadRequest

def register(request):
    if request.method == 'POST':
        user = User()
        user.fname = request.POST['fname']
        user.lname = request.POST['lname']
        user.email = request.POST['email']
        user.password = request.POST['password']
        user.repassword = request.POST['repassword']
       
        if user.password != user.repassword:
            return redirect("register")
        elif user.fname == "" or user.password == "" :
            messages.info(request,f"some fields are empty")
            return redirect("register")

        user.save()
        return render(request, 'register.html')
    else:
        raise BadRequest()  # Or do something else

This view will work with POST requests.

  • Related