I have a page that has a list of questions, in a table, with a radio button to select your choice, with values 0 - 5 assigned to each of the buttons. Each question is assigned a name= the question ID.
I want to use the POST data to populate the answers into my SQLLite3 DB.
I can extract the values and use it for calculations, but if I try to save the form it always comes up Invalid. The problem appears to be the data is somehow blank when I try to retrieve it by question.id; which I think is the key for the values.
model.py
class Question(models.Model):
question = models.CharField(max_length=200)
readonly_fields=(“id”)
#name = models.CharField(max_length=20, unique=True) # THis is probably uselesstopic = models.ForeignKey( Topic, on_delete=models.CASCADE, null=True ) # Django will store this as topic_id on database def __str__(self): return f"{self.question}"
class Answer(models.Model):
value = models.IntegerField(null = True)
#name = models.CharField(max_length=20, null = True)
# q_id = models.CharField(max_length=10, default=“XX”) # USed foreignkey instead
question = models.OneToOneField(
Question, on_delete=models.CASCADE, null = True,
)def __str__(self): return f"{self.question} value is {self.value}"
class Comment(models.Model):
name = models.CharField(max_length=20)
comments = models.CharField(max_length=1000, default=“No comment”)
area = models.ForeignKey(
Area, on_delete=models.CASCADE, null = True
)
forms.py
from django import forms
from .models import Answer, Comment, Question, Area, Topicclass AnswerForm(forms.ModelForm):
class Meta:
model = Answer
fields = “all”
views.py snippet using the form:
if request.method == “POST”:
keys=request.POST.keys()
values=list(request.POST.values())
print("Keys: ",keys, “values: “, values)
value1=request.POST.get(“question.id[1]”,””)
print("Value1: ", value1)
form=AnswerForm(request.POST)
if form.is_valid():
print(“Valid”)
form.save()
else:
print(“Invalid”)
questions.html: (Edited out repeated for loops to generate table rows)
{% extends “layout.html” %}
{% block content %}
div.item_leadership { background-color: rgb(211,211,211); } {% csrf_token %}{{ areaheader.1}}
{% for question in questions1 %} <tr> <td class="question">{{question}}</td> {% for n in nchoices %} {% if n == 0 %} <td> <input name= {{question.id}} type="radio" value={{ n }} id="{{name}}" /><lable> Not Sure</lable> </td> {% else %} <td> <input name={{question.id}} type="radio" value={{ n }} id="{{name}}" /><lable> {{n}}</lable> </td> {% endif %} {% endfor %} </tr> {% endfor%} </table> <h4>Enter any comments about your responses to the questions</h4> <textarea name="{{ textname }}">"Enter Comments Here"</textarea > <input type="submit">
{% endblock %}
Results from print statements:
Keys: dict_keys([‘csrfmiddlewaretoken’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘10’, “[‘L_Text’]”]) values: [‘Gll91n4FY9UrF10WjlnKeEiHhJCFCIFEzZvGZVLQK5aoppfAQgsHRzcjuxuWGcxt’, ‘1’, ‘2’, ‘3’, ‘3’, ‘4’, ‘4’, ‘4’, ‘5’, ‘5’, ‘5’, ‘“Enter Comments Here”’]
Value1:
Invalid
{{ columnheader.0}} {{ columnheader.1}} {{ columnheader.2}} {{ columnheader.3}} {{ columnheader.4}} {{ columnheader.5}} {{ columnheader.6}} {{ topic.0 }}