Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use ListView in Django

Shulou Source: shulou.com Published: 2022-06-02 09:56:19 10月28日 Update

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.

Tags: Data code scenarios users ways methods more content articles knowledge articles restrictions good convenient concise one line above not only information reverse order Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information NVidia Shulou Technology Docker