Home > Mobile >  How to use the attribute : save() in views.py?
How to use the attribute : save() in views.py?

Time:12-31

** When I use the attribute save() in views.py the page gets this error : NOT NULL constraint failed: pages_login.username

# views.py  :
    def about(request):
        username = request.POST.get("username")
        password = request.POST.get("password")
        data = Login(username=username,password=password)
        data.save()
        return render(request, 'pages/about.html')
# models.py :

    class Login(models.Model):
        username=models.CharField(max_length=40)
        password=models.CharField(max_length=40)

**

CodePudding user response:

Login(username=username,password=password)

This code will automatically insert the data to db. Please check your database. Or else you can save like this

data = Login()
data.username = username
data.password = password
data.save()

Always save password as hashed form for security

CodePudding user response:

is this the login view? no need to save you just have to redirect user after authenticating him

CodePudding user response:

Try this:

username = request.POST.get("username")
password = request.POST.get("password")

Login.objects.create(username=username, password=password)

This statement will create and save object in single step.

CodePudding user response:

I have changed the code without getting the error,and the problem was solved. instead of data.save() I put :

if request.POST.get("username") :
    data.save()

But I still don't know what is happening , do you know what is happening in the program ? I think that it can't save nothing so the page gets an error, but when I put (if ...) I order to save only when there is a username to get.

  • Related