Django in DetailView How to receive multiple parameter values passed in a URL

Source: Internet
Author: User

Suppose there are model:poke and card, where poke is the foreign key of the card. Our URL has such a situation, POKE/2/CARD/1, that is, the 1th card in the Poke ID 2, we need to get the incoming 2 and 1 in the URL, we use Class View to complete.


First Dtailview, where the Get_objet () method obtains an object by taking the parameter passed in the URL. For example, POKE/2, the Get_object () is pk=2 through the Poke model (where PK is the default named group in the DetailView, we can rewrite it, expand it, like the following) to get a corresponding poke.

So, how do you get that 2?

View the source, in the Singleobjectmixin class attribute, there are

Pk_url_kwarg = ' PK '

In the Get_object () method, there are:

    def get_object (self, queryset=none):         "" "        Returns the


The object the view is displaying.         By default this requires ' Self.queryset ' and a ' pk ' or ' slug ' argument       &N Bsp
In the URLconf, but subclasses can override this to return any object.         "" "        # Use a custom Queryset if provided; This is required to subclasses         # like Datedetailview         if Queryset is None:             Queryset = Self.get_queryset ()         # Next, tr
Y looking up by primary key.         PK = Self.kwargs.get (Self.pk_url_kwarg, None)         slug = SELF.KWARGS.G ET (Self.slug_url_kwarg, None)         if PK. Not None:           que Ryset = Queryset.filter (PK=PK)

From the Get method, we can see that PK is the Get_object () method used to filter our Queryset parameters.

and PK This variable, is through the Kwargs get method, which Pk_url_kwarg = ' PK ', that is, pk = Self.kwargs.get (Self.pk_url_kwarg, None) is to get the URL, The named group of incoming parameters is the value of PK and is passed to the local variable PK. So, to get the arguments passed in from any URL, just use Kwargs.get (named Group, None), and the named group is defined in the URL, OK.


So, like a URL like this:

URL (r ' ^poke/(?) p<poke_pk>\d+)/card/(? p<pk>\d+)/$ ', Cardinfo.as_view (), name= ' Cardinfo '),

The actual URL, such as

Poke/1/card/2

We can do this in our Class View Cardinfo

  Class Cardinfo (Pokeinfo):
      model=card
      context_object_name= ' information ' template_name= ' cardspace/
      Card.html '
      pk_poke_kwargs= ' poke_pk '
      def get_object (self,queryset=none):
          cnum=int (Self.kwargs.get ( Self.pk_url_kwarg,none))
          Pnum=int (Self.kwargs.get (self.pk_poke_kwargs,none))
          Query=self.get_queryset ()
          Try:
              obj=query[pnum-1].cards.all () [cnum-1]
          except Indexerror:
              raise Http404 return
          obj
      def get_context_data (Self,**kwargs):
          context=detailview.get_context_data (Self,**kwargs)
          return Context


Where, Pk_poke_kwargs is our own definition of the second parameter incoming URL naming group, the name is arbitrary, but the value must be the same as the named group passed in the URL

Num=int (Self.kwargs.get (Self.pk_url_kwarg,none)) is that we follow the Django default parameter method to get the named group in the URL of the PK parameter, get 2, then cnum=2

After that, we get the value of the POKE_PK in the named group:

Pnum=int (Self.kwargs.get (Self.pk_poke_kwargs,none))


That's OK.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.