Home > Enterprise >  Displaying Django form answers
Displaying Django form answers

Time:03-10

I couldn't find a solution to this on the Django website, currently I have a form as shown:

It saves to my models.py like the following:

What I want to do is use the answers from a form and create a new form using the answers of this form as the questions for it, basically recreating this form and reusing the fields but changing the fields and displaying it on a unique URL

class InitialForm(forms.Form):
    Teacher_Name = forms.CharField(label='Teacher Name')
    Test_Name = forms.CharField(label='Label this test')
    Subject = forms.CharField(label = 'Subject')
    Question = forms.CharField(label = 'What is the first question?')
    Topic = forms.CharField(label = 'What topic is this on?')
    Option1 = forms.CharField(label = 'What is the first option?')
    Option2 = forms.CharField(label = 'What is the second option?')
    Option3 = forms.CharField(label = 'What is the third option?')
    Option4 = forms.CharField(label = 'What is the fourth option?')
    Answer = forms.CharField(label = 'Which option is the correct option?', widget=forms.Select(choices=Options))

class Questions(models.Model):
    testID = AutoSlugField(unique=True)
    test_label = models.CharField(max_length=1000)
    teacherName = models.CharField(max_length=1000, null = True)
    studentID = models.ForeignKey(Student, on_delete=models.CASCADE, null = True)
    Subject = models.CharField(max_length=1000)
    Q1 = models.CharField(max_length=1000, null = True)
    Topic1 = models.CharField(max_length=1000)
    Option1_Q1 = models.CharField(max_length=1000)
    Option2_Q1 = models.CharField(max_length=1000)
    Option3_Q1 = models.CharField(max_length=1000)
    Option4_Q1 = models.CharField(max_length=1000)
    AnswerQ1 = models.CharField(max_length=1000)
    ``

CodePudding user response:

You need to step back and think about what objects you are going to store in your database amd what you do with them, and most importantly, how they are related. Getting the relationships right as early as possible is the most important thing, because it's hardest to change later.

I would guess that there's something like a Test which has Questions, and that each Student will be sumbitting Answers to the Test.

Question objects will have a ForeignKey relationship to a Test. Answer objects will have a ForeignKey relationship both to a Test and to a Student.

You now have a whole set of views to write. There will be CreateView and UpdateView (and probably DeleteView) that an (authenticated!) teacher will use to manipulate the Question objects for a particular Test. The text of the Question and the option details will be displayed to each Student sitting the test; the student will respond with his choice for the right answer which will be recorded as an Answer object`

The student-answer form will input just one field: the student's chosen answer (A to D, or 1 to 4?) What the student sees will be a display of a Question object: a read-only view of the Question data which was input by the teacher.

Chances are that after you get the hang of this you will be introducing Teacher and Course objects (multiple Test objects linked to one Course. one or more Teacher objects linked to one Course, ...)

Opinionated, but this is exactly the sort of problem that Class-Based Views make easy, so use them.

  • Related