我正在创建多项选择测验,但无法将所选选项保存在数据库中的单选按钮上作为新答案。另外,我希望您显示该选项是否与正确答案匹配。 当我选择其中一个选项时,它会在地址栏中显示文本旁边的单选按钮的值,但我希望将此值保存在db中。
views.py
def detalhes(request, idProva):
prova = get_object_or_404(Prova,pk=idProva)
model = Prova
template_name = 'polls/detalhes.html' # Default: <app_label>/<model_name>_list.html
context_object_name = 'prova' # Default: object_list
paginate_by = 10
queryset = Prova.objects.all()
usuario = request.user.id
questao = request.POST.get('idQuestao')
try:
questao_selec = prova.questao_set.get(pk=request.POST['questao'])
pagina = paginator.page(page)
resposta = Resposta(resposta=resposta,idQuestao=idQuestao,idResposta=1,respostacerta=1)
resposta.save()
return redirect('/')
except (KeyError,Questao.DoesNotExist):
return render (request,'polls/detalhes.html',{
'prova': prova,
})
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
else:
#questao_selec.votes += 1
questao_selec = Questao(textoQuestao=request.POST.get('textoQuestao'),imagemQuestao=request.FILES('imagemQuestao'),imagem2Questao=request.FILES('imagem2Questao'),perguntaQuestao=request.POST.get('perguntaQuestao'))
resposta_selec = Resposta(idResposta = request.POST.get('idResposta'),idQuestao = request.POST.get('idQuestao'),usuario = request.POST.get('usuario'),resposta = request.POST.get('#mysubmit'))
questao_selec.save()
resposta_selec.save()
return HttpResponseRedirect(reverse('polls:resultados',args=(prova.idProva,)))
detalhes.html
<form id="NPSform" method="GET">
{% if not resposta.usuario %}
<input type="radio" name="scores" id="A" value="A"> A) {{questao.aOpcao}}<br>
<input type="radio" name="scores" id="B" value="B"> B) {{questao.bOpcao}}<br>
<input type="radio" name="scores" id="C" value="C"> C) {{questao.cOpcao}}<br>
<input type="radio" name="scores" id="D" value="D"> D) {{questao.dOpcao}}<br>
<input type="radio" name="scores" id="E" value="E"> E) {{questao.eOpcao}}<br>
<input type="submit" style="display: none;" name="mysubmit" value='{{resposta.resposta}}'>
<button id="btnresposta" class="btn btn-primary" >Selecionar resposta</button>
{% else %}
{{ resposta.resposta }}
{% endif %}
</form>
models.py
class Questao(models.Model):
idQuestao = models.CharField(max_length=7,primary_key=True,null=False)
idProva = models.ForeignKey(Prova,on_delete=models.CASCADE)
idCategoria = models.ManyToManyField(Categoria,default="Categorias")
textoQuestao = models.CharField(max_length=2000,blank=True,null=True)
imagemQuestao = models.FileField(upload_to='static/img/uploads',blank=True,null=True)
imagem2Questao = models.FileField(upload_to='static/img/uploads',blank=True,null=True)
perguntaQuestao = models.CharField(max_length=500,blank=True,null=True)
aOpcao = models.CharField(max_length=500,null=False,blank=True)
bOpcao = models.CharField(max_length=500,null=False,blank=True)
cOpcao = models.CharField(max_length=500,null=False,blank=True)
dOpcao = models.CharField(max_length=500,null=False,blank=True)
eOpcao = models.CharField(max_length=500,null=False,blank=True)
respostaQuestao = models.CharField(max_length=1)
STATUS_QUESTAO_CHOICES = (
(UM,'Ativa'),
(DOIS,'Inativa'),
)
statusQuestao = models.IntegerField('Status de questão',choices=STATUS_QUESTAO_CHOICES,null=False)
QUESTAO_CHOICES = (
(UM,'Múltipla escolha'),
(DOIS,'Discursiva'),
)
tipoQuestao = models.IntegerField('Tipo de questão',choices=QUESTAO_CHOICES,null=False)
def __str__(self):
return self.idQuestao
class Meta:
verbose_name = "Questão"
verbose_name_plural = "Questões"
class Resposta(models.Model):
idResposta = models.CharField(max_length=9,primary_key=True,null=False)
idQuestao = models.ForeignKey(Questao,on_delete=models.CASCADE)
usuario = models.ForeignKey(User,on_delete=models.CASCADE)
resposta = models.CharField(max_length=1)
certaresposta = models.BooleanField(default=True)
def __str__(self):
return self.idResposta
答案 0 :(得分:0)
您正在访问request.POST
,但您的表单中有method=GET
。