Home > front end >  return None form django
return None form django

Time:05-16

i have a form fill for register but when i try to save it it involves a none object.

enter image description here

the problem is :

The view pages.views.essai didn't return an HttpResponse object. It returned None instead.

here is my code :

views.py

  from django.shortcuts import redirect, render
  from django.template   import RequestContext
  from .models import Login

def essai(request):

if not request.method == 'POST':

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

 data = Login(username,password)
 print(data)

 return render(request,'pages/essai.html')

urls.py

from django.urls import URLPattern, path
from . import views

urlpatterns = [
 path('essai', views.essai, name = 'essai'),  

]

essai.html

<form   action=""  method ="POST" >
{% csrf_token %}                    
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value ="Save">
</form>

admin.py

from django.contrib import admin
from .models import Login

admin.site.register(Login)

model.py

from django.db import models

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

PS : I tried with the forms but it's the same problem.

I do not understand where is the problem is it the views or the model

CodePudding user response:

I think the if condition is the opposite. You wrote if not but I think it should be if.

from django.shortcuts import redirect, render
from django.template   import RequestContext
from .models import Login

def essai(request):

    if request.method == 'POST':  // here

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

        data = Login(username,password)
        print(data)

        return render(request,'pages/essai.html')

CodePudding user response:

Your model seems correct. The issue relating to the error message you received is that your view method is checking whether request is not POST. When sending data from the browser to the server you are "posting" so your view should be listening for POSTs when accessing data from your HTML form and assigning them to python variables. You also don't actually save your Login object but only just create it in memory. If you call data.save() it will trigger a database transaction and attempt to add it to your Login table.

When you initially navigate to the page you trigger a GET method as your console output indicates and so you also need to check for request.GET in your view method. In your case this should just return a blank HttpResponse via render() as you do in your post check. A more complete view function might be:

from django.shortcuts import redirect, render
 from django.template import RequestContext
 from .models import Login

def essai(request):

  if request.method == 'POST':
    username = request.POST.get('username')
    password = request.POST.get('password')

    data = Login(username,password)
    print(data)
    data.save() # to save the object to the database if needed.

    return render(request,'pages/essai.html')

  elif request.method == 'GET':
    return render(request,'pages/essai.html')

The check for request.GET might become useful later when you begin needing to modify how your template looks when it loads for the first time.

  • Related