This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from . import apiviews | |
| path('questions/', apiviews.questions_view, name='questions_view') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mysite/urls.py | |
| path('api/polls/', include('polls.urls')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| touch polls/apiviews.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| path('questions/<int:question_id>/result/', apiviews.question_result_view, name='question_result_view') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['GET']) | |
| def question_result_view(request, question_id): | |
| question = get_object_or_404(Question, pk=question_id) | |
| serializer = QuestionResultPageSerializer(question) | |
| return Response(serializer.data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChoiceSerializerWithVotes(ChoiceSerializer): | |
| class Meta(ChoiceSerializer.Meta): | |
| fields = ChoiceSerializer.Meta.fields + ('votes',) | |
| class QuestionResultPageSerializer(QuestionListPageSerializer): | |
| choices = ChoiceSerializerWithVotes(many=True, read_only=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['PATCH']) | |
| def vote_view(request, question_id): | |
| question = get_object_or_404(Question, pk=question_id) | |
| serializer = VoteSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| choice = get_object_or_404(Choice, pk=serializer.validated_data['choice_id'], question=question) | |
| choice.votes += 1 | |
| choice.save() | |
| return Response("Voted") | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| path('questions/<int:question_id>/vote/', apiviews.vote_view, name='vote_view'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class VoteSerializer(serializers.Serializer): | |
| choice_id = serializers.IntegerField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['GET', 'POST']) | |
| def questions_view(request): | |
| if request.method == 'GET': | |
| questions = Question.objects.all() | |
| serializer = QuestionListPageSerializer(questions, many=True) | |
| return Response(serializer.data) | |
| elif request.method == 'POST': | |
| serializer = QuestionSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| question = serializer.save() |