taking values from user through signup page using ModelForm in django -
i making signup page using django modelform. models.py
from django.db import models 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 django.http import httpresponseredirect .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 account</h1> {{form}} <input type="submit" value="submit" /> </form> </section> </div> </body> </html>
but displays blank form "create account" , "submit" button when access /student/signuppage.html ...there no textboxes or dropdowns...aren't supposed generated automatically?
do need make forms.py modelforms also?...if yes put inside that?
this because make get
request view, closely how structured:
def get_userdetails(request): if request.method == 'post': # ... if form.is_valid(): # ... else: # ... return render(request, 'student/homepage.html', {'form':form})
you can see there no handling of happens when request.method
different post
.
actually, there is, have written it, need move else
statement outer if
(if request.method == 'post'
). in django docs there same example.
so need make view like:
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}) # <<<
Comments
Post a Comment