Делаю опросник, нужно в форме вывести все вопросы и варианты ответа к ним, после выбора нужно сохранить результаты в отдельную модель, как это можно реализовать?
html с формой:
{% extends 'poll_api/base.html' %}
{% block title %}
{{ polls.poll_name }}
{% endblock %}
{% block content %}
<h1 class="mb-5">{{ polls.poll_name }}</h1>
<form action="{% url 'result' question_id %}" method="post">
{% csrf_token %}
{% for question in polls.question_set.all %}
<h2 class="mt-5 mb-5">{{ question }}</h2>
{% for choice in question.answer_set.all %}
<input type="{{ question.type }}" name="choice" id="{{ forloop.counter }}" value="" />
<label for="{{ forloop.counter }}">{{ choice.text }}</label><br />
{% endfor %}
{% endfor %}
<br />
<input type="submit" value="Отправить" />
</form>
{% endblock %}
Модели:
from django.contrib.auth.models import User
from django.db import models
class Poll(models.Model):
poll_name = models.CharField('Название опросника', max_length=200)
def __str__(self):
return self.poll_name
class Question(models.Model):
ONE = 'Radio'
MULTIPLE = 'Checkbox'
choices = (
(ONE, 'Radio'),
(MULTIPLE, 'Checkbox'),
)
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
title = models.CharField('Текст вопроса', max_length=200)
type = models.CharField('Тип вопроса', max_length=20, choices=choices,
default=ONE)
order = models.IntegerField(default=0)
def __str__(self):
return self.title
class Answer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
text = models.CharField('Текст ответа', max_length=200)
order = models.IntegerField(default=0)
def __str__(self):
return self.text
class FillPoll(models.Model):
user = models.ForeignKey(User, related_name='user_answer',
on_delete=models.CASCADE)
answer_choice = models.ForeignKey(Answer, on_delete=models.CASCADE)
def __str__(self):
return self.answer_choice
Views:
from rest_framework import generics, permissions
from . import serializers
from django.contrib.auth.models import User
from .models import Question, FillPoll, Answer, Poll
from .serializers import QuestionSerializer, FillPollSerializer, AnswerSerializer, PollSerializer
from .permisisions import IsAdminOrReadOnly
from django.views import generic
# Poll
class IndexView(generic.ListView):
model = Poll
template_name = 'poll_api/index.html'
context_object_name = 'polls_list'
def get_queryset(self):
return Poll.objects.order_by('id')
class IndexDetailView(generic.DetailView):
model = Poll
template_name = 'poll_api/questions.html'
context_object_name = 'polls'
def get_queryset(self):
# ord = Question.objects.get(pk=self.kwargs['order'])
return Poll.objects.all()
class Qview(generic.DetailView):
model = Question
template_name = 'poll_api/q.html'
lookup_url_kwarg = 'q_order'
context_object_name = 'q'
def get_queryset(self):
p = Poll.objects.get(pk=self.kwargs['pk'])
return Question.objects.filter(poll=p)
# API
class UserList(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = serializers.UserSerializer
class UserDetail(generics.RetrieveAPIView):
queryset = User.objects.all()
serializer_class = serializers.UserSerializer
class PollList(generics.ListCreateAPIView):
queryset = Poll.objects.all()
serializer_class = PollSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsAdminOrReadOnly]
class PollDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Poll.objects.all()
lookup_url_kwarg = 'poll_id'
serializer_class = PollSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsAdminOrReadOnly]
class QuestionList(generics.ListCreateAPIView):
serializer_class = QuestionSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsAdminOrReadOnly]
def get_queryset(self):
p = Poll.objects.get(pk=self.kwargs['poll_id'])
return Question.objects.filter(poll=p)
class QuestionDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = QuestionSerializer
lookup_url_kwarg = 'question_id'
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsAdminOrReadOnly]
def get_queryset(self):
p = Poll.objects.get(pk=self.kwargs['poll_id'])
return Question.objects.filter(poll=p)
class AnswerList(generics.ListCreateAPIView):
serializer_class = AnswerSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsAdminOrReadOnly]
def get_queryset(self):
q = Question.objects.get(pk=self.kwargs['question_id'])
return Answer.objects.filter(question=q)
class AnswerDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = AnswerSerializer
lookup_url_kwarg = 'answer_id'
permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsAdminOrReadOnly]
def get_queryset(self):
q = Question.objects.get(pk=self.kwargs['question_id'])
return Answer.objects.filter(question=q)
class FillPollList(generics.ListAPIView):
queryset = FillPoll.objects.all()
serializer_class = FillPollSerializer