3/8/2023 0 Comments The button part 4order_by ( '-pub_date' ) class DetailView ( generic. ListView ): template_name = 'polls/index.html' context_object_name = 'latest_question_list' def get_queryset ( self ): """Return the last five published questions.""" return Question. models import Choice, Question class IndexView ( generic. This reverse() call will return a string likeįrom django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.views import generic from. In thisĬase, using the URLconf we set up in Tutorial 3, Variable portion of the URL pattern that points to that view. It is given the name of the view that we want to pass control to and the This function helps avoid having to hardcode a URL in the view function. HttpResponseRedirect constructor in this example. We are using the reverse() function in the This tip isn’t specific to Django it’s good web development HttpResponseRedirect after successfully dealing with URL to which the user will be redirected (see the following point for howĪs the Python comment above points out, you should always return an HttpResponseRedirect takes a single argument: the HttpResponseRedirect rather than a normal KeyError and redisplays the question form with an errorĪfter incrementing the choice count, the code returns an Request.POST will raise KeyError ifĬhoice wasn’t provided in POST data. Note that Django also provides request.GET for accessing GET data in the same way –īut we’re explicitly using request.POST in our code, to ensure that data is only Request.POST returns the ID of the selected choice, as a Object that lets you access submitted data by key name. This code includes a few things we haven’t covered yet in this tutorial: return HttpResponseRedirect ( reverse ( 'polls:results', args = ( question. This prevents data from being posted twice if a # user hits the Back button. save () # Always return an HttpResponseRedirect after successfully dealing # with POST data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |