I am making a signup page using django ModelForm. models.py
from django.db import models
from django.forms import ModelForm
country_choices=(
('india','INDIA'),
('egypt','EGYPT'),
('germany','GERMANY'),
('afghanistan','AFGHANISTAN'),
('switzerland','SWITZERLAND'),
('usa','USA'),
('mexico','MEXICO'),
)
class user(models.Model):
uid=models.IntegerField(unique=True,default=0)
uname=models.CharField(max_length=50)
email=models.EmailField()
password=models.CharField(max_length=20)
phoneno=models.IntegerField(default=0)
addr1=models.CharField(max_length=50)
addr2=models.CharField(max_length=50)
city=models.CharField(max_length=20)
state=models.CharField(max_length=20)
country=models.CharField(max_length=20,choices=country_choices)
pincode=models.IntegerField(default=0)
securityq=models.CharField(max_length=100)
securitya=models.CharField(max_length=100)
def __unicode__(self):
return self.uid,self.uname
class Meta:
db_table="user"
class userForm(ModelForm):
class Meta:
model= user
fields= ['uid','uname','email','password','phoneno','addr1','addr2','city','state','country','pincode','securityq','securitya']
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import userForm
def homepage(request):
return render(request,'student/homepage.html',)
def signuppage(request):
return render(request,'student/signuppage.html',)
def get_userdetails(request):
if request.method=='POST':
form=userForm(request.POST)
if form.is_valid():
new_form=form.save()
return HttpResponseRedirect('student/signuppage.html')
else:
form=userForm()
return render(request,'student/homepage.html',{'form':form})
signuppage.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>Sign Up</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{% static 'student/signupcss.css' %}" />
</head>
<body>
<div class="container">
<section id="content">
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h1>Create an Account</h1>
{{form}}
<input type="submit" value="Submit" />
</form>
</section>
</div>
</body>
</html>
But it displays a blank form with only "Create my account" and a "Submit" button when i access /student/signuppage.html ...there are no textboxes or dropdowns...aren't they supposed to be generated automatically?
Do i need to make forms.py with Modelforms also?...if yes then what would i put inside that?
This is because you make a
GET
request to your view, but look closely how it is structured:You can clearly see that there is no handling of what happens when
request.method
is different fromPOST
.Actually, there is, you have written it, but you need to move the
else
statement to be for the outerif
(if request.method == 'POST'
). In the Django docs there is the same example.So you need to make your view like: