1

I am trying to build a grading system. I have a class called Score which lists all the scores of students.

class Score(Base):
    student = models.ForeignKey(Usr, limit_choices_to={'user_type': 'Student'}, related_name='scored')
    subject = models.ForeignKey(Subject)
    teacher = models.ForeignKey(Usr, limit_choices_to={'user_type': 'Teacher'}, related_name='marked')
    exam = models.CharField(max_length=50)
    exam_date = models.DateField()
    score = models.IntegerField()
    out_of = models.IntegerField()

The view currently passes two objects, student_list and subject_list, which carries the number of student in a particular class, and the number of subjects that the logged in teacher takes. This will allow him to choose which subject he is marking for that particular group of students.

def Edit(request, pk):
    this_klass = Klass.objects.get(id=pk)
    student_list = this_klass.kara_pore.all()
    subject_list = request.user.teaches.subject.all
    return render(request, "grades/edit.html", {'student_list': student_list, 'subject_list': subject_list})

The template displays a table, where the farthest left column has the student list, and then the next column has textfields for submitting their scores. I have named the textfields as the id of the corresponding student, hoping that would help me to reference them while saving the scores.

{% extends "base.html" %}

{% block body %}
<form method="POST" action="process/"> {%  csrf_token %}
    <select name="sub">
    {% for subject in subject_list %}
        <option value="{{ subject }}">{{ subject }}</option>
    {% endfor %}
    </select>
<br/>
    {% for student in student_list %}
        {{ student }}<input type="text" name="{{ student.student_id}}"/> <br/>
    {% endfor %}

    <input type="submit" value="Submit"/>
</form>
{% endblock %}

I currently have no idea how I can process the entry of the scores and save them in the Score table in reference to specific students. Till now I have only saved one row in Django. More specifically, am I saving the data the wrong way? Do I have to use arrays for this type of problems?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MiniGunnR
  • 5,590
  • 8
  • 42
  • 66

2 Answers2

1

There are really many ways to deal with this. For example, you could simply process your html form in the view, in which you would manually create or update all ORM instances.

I believe though that it would be very beneficial for you to take a good read on Django forms and modelforms. The above task could be easily accomplished by using a modelform on the Score model and any other models.

Also this answer explains how to deal with multiple django forms on a single post request.

Community
  • 1
  • 1
Wtower
  • 18,848
  • 11
  • 103
  • 80
  • If I use a ModelForm, will it not provide me a form to make a single entry for a single student? This will be tedious job for the user, because he has to select the class, the subject, the student, and the score for each entry. I was thinking that someone can just put in the score for all students at once and fill the score column in the html form, and then just hit the Submit button. This would insert data in multiple rows where the class, subject, etc. data would be constant but based on the textfield name the student id would be different. Does that make any sense? :-/ – MiniGunnR Jun 12 '15 at 12:33
  • Well, you can provide default values and only update the fields that matter. You don;t have to follow the basic examples only. – Wtower Jun 12 '15 at 12:35
1

here is my code to deal with the table in diagnose_app, believe it would help for you.

#get the distinct time, blade and counts name info
def gettableinfo(request,tablename):
    tableinfo={}
    datedis=[]
    bladedis = []
    counts = []
    if request.is_ajax():
        if request.method=='GET':
            for m in get_models(get_app('diagnose_app'), include_auto_created=True):
                if m._meta.db_table == request.GET.get('tablename'):
                    for bd in m.objects.values('STATION_ID').distinct():
                        bladedis.append(bd['STATION_ID'])
                    for dt in m.objects.values('Day','Time').distinct():
                        datedis.append(dt['Day'].isoformat()+' '+dt['Time'].isoformat())
                    for ct in m._meta.get_all_field_names():
                        if ct.isupper():
                            counts.append(ct)

                    #tableinfo.append(m._meta.get_field('Blade'))
                    tableinfo['tablename'] = request.GET.get('tablename')
                    tableinfo['blades'] = bladedis
                    tableinfo['times'] = datedis
                    tableinfo['counts'] = counts
                    #tableinfo=tableinfo.append('abcde')
                    #table_info_json=json.dumps(tableinfo)
                    table_info_json=json.dumps(tableinfo)
    return HttpResponse(table_info_json,content_type="application/json") 
Okazari
  • 4,597
  • 1
  • 15
  • 27
Allen211
  • 69
  • 6