Beginner django2-Get URL parameters in class-based views

Source: Internet
Author: User

Currently you want to implement the function is to click on two links: in-service staff, leaving staff. Two pages are queried using the same listview.

The link for the first-in-service staff is

http://127.0.0.1/HRSystem/personlist/is_deleted-0

The link for the departing person is

Http://127.0.0.1/HRSystem/personlist/is_deleted-1

Is_deleted is the name of the field that is deleted in the model.

If you follow the links above, you can define urls.py:

urls.py from
hrsystem.views import Personlistview
to Django.contrib.auth import views as auth_views, Decorators	 
url (r ' ^personlist/([\w]+)-([\w]+)/$ ',
	 decorators.login_required (Personlistview.as_view (), Login_url= ' Hrsystem:login '),
	 name= ' personlist '),

The above uses the adorner login_require, if not logged in to visit the page, it will jump to the landing page.


Personlist class defined in views (omit references here)

views.py
class Personlistview (generic. ListView):
	template_name = ' hrsystem/personlist.html '
	paginate_by = +

	def get_queryset (self,*args,** Kwargs):
		 person_list = Person.objects.filter (is_deleted=self.args[1])
		 return person_list

	def get_ Context_data (Self,**kwargs):
		context = Super (personlistview,self). Get_context_data (**kwargs)
		return Context
It is worth noting that although the Is_deleted field uses Boonleanfield, true/false can not be substituted for 0 or 1 when URL access is used.

Self.args[] Gets the parameters in the URL.


If you do not want to pass parameters through args, you can use Kwargs to pass, but the URL is changed to:

URL (r ' ^personlist/(? p<a>[\w]+)-(? p<b>[\w]+)/$ ',
	 decorators.login_required (Personlistview.as_view (), login_url= ' Hrsystem:login ')
	 , Name= ' Personlist '),
Add parameter name A in the URL, b then in the reference to the views can be directly from the parameter name in the method to obtain

	def get_queryset (Self,*args,**kwargs):
		 person_list = Person.objects.filter (is_deleted=self.kwargs[' B '])
		 Return person_list

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.