我已经为学生的markdata输入开发了一个Django项目。不幸的是,条目应针对每个主题和每个问题。每个主题中的问题数量都不同。为了方便进行数据输入,我计划在提交时在html表中放置多个文本框,这些多个文本框的值应该用逗号连接并存储在数据库的单个字段中。
我提供了以下视图,模型和模板代码:
views.py:
def savemark(request):
if request.POST:
subject=request.POST.get("subject")
subdata = Subject.objects.filter(subid=subject)
course=request.POST.get("course")
year=request.POST.get("year")
cohart=request.POST.get("cohart")
exam=request.POST.get("exam")
examdata = Exam.objects.filter(examid=exam)
qddata=Questiondetails.objects.filter(subid=subject,examtype=exam)
studata=Studentcohart.objects.filter(cohartid=cohart,currentyear=year).values('studentid')
return render(request,'blog/savemarks.html',{'student':studata,'question':qddata,'subject': subdata,'exam':examdata,'course':course,'year':year,'cohart':cohart})
else:
return render(request,'blog/home.html')
models.py
class Mark(models.Model):
year = models.CharField(max_length=20)
mark = models.IntegerField()
subid = models.ForeignKey('Subject', models.DO_NOTHING, db_column='subID') # Field name made lowercase.
studentid = models.ForeignKey('Student', models.DO_NOTHING, db_column='studentID') # Field name made lowercase.
examid = models.ForeignKey(Exam, models.DO_NOTHING, db_column='examID') # Field name made lowercase.
cohartid = models.ForeignKey(Cohart, models.DO_NOTHING, db_column='cohartID') # Field name made lowercase.
class Studentcohart(models.Model):
studentid = models.ForeignKey(Student, models.DO_NOTHING, db_column='studentID') # Field name made lowercase.
currentyear = models.CharField(max_length=100)
cohartid = models.ForeignKey(Cohart, models.DO_NOTHING, db_column='cohartID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'studentcohart'
def __str__(self):
return u'%s %s %s' % (self.studentid,self.currentyear,self.cohartid)
class Subject(models.Model):
subid = models.CharField(db_column='subID', primary_key=True, max_length=50) # Field name made lowercase.
subname = models.CharField(db_column='subName', max_length=100) # Field name made lowercase.
year = models.CharField(max_length=20)
cohartid = models.CharField(db_column='cohartID', max_length=20) # Field name made lowercase.
courseid = models.ForeignKey(Course, models.DO_NOTHING, db_column='courseID') # Field name made lowercase.
staffname = models.CharField(db_column='staffname', max_length=50)
class Meta:
managed = False
db_table = 'subject'
def __str__(self):
return u'%s %s %s %s %s %s' % (self.cohartid,self.year,self.courseid,self.subid,self.subname,self.staffname)
class Questiondetails(models.Model):
id = models.CharField(db_column='id', primary_key=True, max_length=6)
year = models.CharField(max_length=20)
totalnoofquestions=models.CharField(max_length=20)
MCQ=models.CharField(max_length=20)
SA=models.CharField(max_length=20)
SBA=models.CharField(max_length=20)
subid = models.ForeignKey('Subject', models.DO_NOTHING, db_column='subjectID') # Field name made lowercase.
examtype = models.ForeignKey(Exam, models.DO_NOTHING, db_column='examType') # Field name made lowercase.
cohartid = models.ForeignKey(Cohart, models.DO_NOTHING, db_column='cohartID') # Field name made lowercase.
courseid = models.ForeignKey(Course, models.DO_NOTHING, db_column='courseID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'Questiondetails'
def __str__(self):
return u'%s %s %s %s %s %s %s %s %s %s' % (self.id,self.cohartid,self.year,self.examtype,self.courseid,self.subid,self.totalnoofquestions,self.MCQ,self.SBA,self.SA)
class Exam(models.Model):
examid = models.IntegerField(db_column='examID', primary_key=True) # Field name made lowercase.
examname = models.CharField(db_column='examName', max_length=20) # Field name made lowercase.
type = models.CharField(max_length=50)
class Meta:
managed = False
db_table = 'exam'
def __str__(self):
return u'%s %s %s' % (self.examid,self.examname,self.type)
savemarks.html
<html>
<body>
<table>
<form action="/submit/" method="post" >
<tr>
<th>studentid</th>
{% for i in "x"|rjust:"30" %}
<th>Q{{forloop.counter}}</th>
{% endfor %}
</th>q{{forloop.counter}}<th>
</tr>
{% for sub in student%}
<tr>
<td>
{{ sub.studentid}}
</td>
{% for i in "x"|rjust:"30" %}
<td>
<input type="text" name="mark" /><br />
</td>
{% endfor %}
</tr>
{% endfor %}
<table>
<input type="submit" value="Log In"/>
</form>
<p><a href="{% url 'logout' %}">Log out</a></p>
</body>
</html>
我不知道如何实现将字段拆分为多个文本框并将数据收集回db。请有人指导我如何实现这一目标。预先感谢