text: from django.http.response import Http404, HttpResponseRedirect
  from django.shortcuts import get_object_or_404, render
  from django.urls import reverse
  from .models import Question, Choice
  
  # Create your views here.
  
  
  # Get questions and display them
  def index(request):
  
      # Getting latest 5 questions, ordering in descending order according to pub_date
      latest_question_list = Question.objects.order_by("-pub_date")[:5]
  
      # Storing the questions in a dictionary
      context = {"latest_question_list": latest_question_list}
  
      # rendering index templating and passing the data to template
      return render(request, "polls/index.html", context)
  
  
  # Show specific question and choices
  def detail(request, question_id):
      try:
          question = Question.objects.get(pk=question_id)
      except Question.DoesNotExist:
          raise Http404("Question doesn't exist")
  
      return render(request, "polls/detail.html", {"question": question})
  
  
  # Get question and display results
  def results(request, question_id):
      question = get_object_or_404(Question, pk=question_id)
      return render(request, "polls/results.html", {"question": question})
  
  
  # Vote for a question choice
  def vote(request, question_id):
      question = get_object_or_404(Question, pk=question_id)
  
      try:
          selected_choice = question.choice_set.get(pk=request.POST["choice"])
      except (KeyError, Choice.DoesNotExist):
          return render(request, "polls/detail.html", {"question": question, "error_message": "You didn't select a choice"})
      else:
          selected_choice.votes += 1
          selected_choice.save()
  
          # Always return HTTPResponseRedirect after successful dealing with POST Data. This prevents data from being posted twice if user hits back button
  
          return HttpResponseRedirect(reverse("polls:results", args=(question_id,)))
  