I am new to python and I am using django for student database application .
Student database application must show id, firstname,lastname,subjectnames,marks.
Single student is having multiple subjects and their marks.
I am getting problem with accessing multiple values that student is having multiple subjects and marks.
models.py
class Person(models.Model):
firstname=models.CharField(max_length=50)
lastname=models.CharField(max_length=50)
def __unicode__(self):
return (self.firstname,self.lastname)
class Marksheet(models.Model):
subname=models.CharField(max_length=50)
marks=models.IntegerField(max_length=10)
person=models.ForeignKey(Person)
def __unicode__(self):
return self.subname
views.py
def add_page(request,page_name): # function for creating the new records
p1=None
p2=None
if request.method=='POST':
p1=Person(firstname=request.POST['firstname'],lastname=request.POST['lastname'])
p1.save()
p2=Marksheet(subname=request.POST.getlist('subnames'),person=Person(person_id))
p2.save()
return render_to_response("add.html",{"page_name":page_name})
creating a records I am using form in html which is shown below….
Templates
add.html
<form method="post" action="/newdcl/{{page_name}}/add/" > {% csrf_token %}
First name: <input type="text" name="firstname" /> <br />
Last name: <input type="text" name="lastname" /> <br />
Operating System <input value="os" name="subnames" type="checkbox"><br />
System Programming <input value="sp" name="subnames" type="checkbox"> <br />
Maths <input value="maths" name="subnames" type="checkbox"> <br />
<input type="submit" value="save" >
</form>
Can anyone help me in this????
Your problem seems to lie in your how you try to create Marksheet, you can’t assign a list of values to one field like that.
Using your currently formless, scary, no-validation, setup… you can do something like this-
You will need to add
blank=True, null=Trueto you marks field in your models.py if you intend to not have any initial mark.Please look at Making Queries and Forms