In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use ListView in Django, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
In general, we query all the data through Django's ORM, and then show it. The code is as follows:
Def user_list (request): "returns all users in UserProfile"users = UserProfile.objects.all () return render (request, 'talks/users_list.html', context= {" user_list ": users})
This can solve the problem, but Django provides a faster and more convenient way for this common scenario, which is ListView, which is used as follows:
From django.views.generic import ListView
Class UsersView (ListView):
Model = UserProfile template_name = 'talks/users_list.html' context_object_name =' user_list'
In this way, we have completed the above function, and the code is very concise.
Scenario 2:
I want to filter the data, how to achieve ListView? The code is as follows:
From django.views.generic import ListView
Class UsersView (ListView):
Model = UserProfile template_name = 'talks/users_list.html' context_object_name =' user_list'
Def get_queryset (self): # override the get_queryset method # to get all users whose is_deleted is False and return data return UserProfile.objects.filter (is_deleted=False) .order_by ('- create_time') in reverse chronological order
If you want to filter the data in more dimensions, for example, if the user is not only a department, but also the gender is male, you can use the Q function provided by Django.
Scene 3
I want to return multiple data to Template, not only user_list, but also other data, such as getting the details of the current login user, what to do? The code is as follows:
From django.views.generic import ListView
Class UsersView (ListView):
Model = UserProfile template_name = 'talks/users_list.html' context_object_name =' user_list'
Def get_context_data (self, * * kwargs): # rewriting the get_context_data method # is critical. You must get the result of the original method to context = super () .get_context_data (* * kwargs) username= self.request.GET.get ('user', None) context [' user'] = UserProfile.objects.get (username=username return context)
This way, when you return to the Template page, the context contains {'user_list': user_list,' user': user}.
Scene 4
I want to restrict the request method of the interface, such as restricting GET access. The code is as follows:
From django.views.generic import ListView
Class UsersView (ListView):
Model = UserProfile template_name = 'talks/users_list.html' context_object_name =' user_list' http_method_names = ['get'] # add this line to tell which method of request is allowed
On how to use ListView in Django to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.