Rohit Chopra Rohit Chopra - 5 months ago 8
Python Question

After filling signup form in html page it gives this error and use username and password and store values in login form

After filling signup form in html page it gives this error and use username and password and store values in login form.:

TypeError at /login/ argument of type 'NoneType' is not iterable.


This is the error

models.py:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class signup(models.Model):
email = models.EmailField();
full_name = models.CharField(max_length=120)
username = models.CharField(_('username'), max_length=20, primary_key = True)
department= models.CharField(max_length=20)
password = models.CharField(_('password'), max_length=128)
pic=models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, default='default.png')
created= models.DateTimeField(auto_now_add=True, auto_now=False)
updated= models.DateTimeField(auto_now_add=False, auto_now=True)

def __str__(self):
return self.username

class login(models.Model):
username = models.CharField(_('username'), max_length=20)
password = models.CharField(_('password'), max_length=128)
login= models.DateTimeField(_('last_login'),auto_now_add=False, auto_now=True)

def __str__(self):
return self.username


forms.py:

from django import forms
from .models import signup
from .models import login

class loginForm(forms.ModelForm):
password=forms.CharField(widget=forms.PasswordInput())
class Meta:
model=login
fields=['username','password']


class SignUpForm(forms.ModelForm):
password=forms.CharField(widget=forms.PasswordInput())
class Meta:
model=signup
fields=['username','full_name', 'email','department','password','pic']


views.py:

from django.shortcuts import render
from .forms import SignUpForm
from .forms import loginForm
from django.db import connection

def home(request):
title= 'welcome'
form = SignUpForm(data = request.POST or None)
print(form.is_valid)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
print(instance)
print(instance.email)
context = {
"title": title,
"form": form,
}
return render(request,"base.html", context)

def login(request):
form = loginForm(data = request.POST or None)
if form.is_valid():
instance = form.save(commit=False)

username=instance.username
password=instance.password
row=auth(username,password)
if password in row:
instance.save()
form1=SignUpForm
context={
"form":form1,
"instance":instance
}
return render(request, "profile.html", context)
title= 'welcome back'
context ={
"title":title,
"form":form
}
return render(request, "login.html", context)

def auth(username,password):
cursor = connection.cursor()
cursor.execute("SELECT password FROM teacher_signup WHERE username = %s", [username])
row = cursor.fetchone()
return row


when click on signup button [if password in row:] this line is called somehow.

signup page

Answer

multiple mistakes in views:

def login(request):
    form = loginForm(request.POST or None) # <-- corrected
    if form.is_valid():
        instance = form.save(commit=False)
        username = instance.username
        password = instance.password
        row = auth(username,password)
        if password in row:
           instance.save()
           form1 = SignUpForm() # <-- you forgot () 
           context={
              "form":form1,
              "instance":instance
           }
           return render(request, "profile.html", context)             

   title= 'welcome back'
   context ={
       "title":title,
       "form":form
   }
   return render(request, "login.html", context)
Comments