Django development blog system (07- customized according to the needs of management background)

Run the system tries to add users and add data, the effect appears

 

 

 

You can see the currently logged in user, although DOCTOR, but other users can still see the article, and the filter also shows the classification of non-current user created, obviously this is a problem we need to solve.

 

Function first address on the right side of the filter, then the need for custom filters, instructions affixed document here

Then we have to write code to customize filters:

. 1  class CategoryOwnerFilter (admin.SimpleListFilter):
 2      "" " Custom filters appear only this user classification " "" 
. 3  
. 4      title = ' classification filter '   # header 
. 5      parameter_name = ' owner_category '   # name of the URL parameter query 
6  
7      DEF Lookups (Self, Request, model_admin):   # return the contents to be displayed and queries of the above mentioned id 
8          return Category.objects.filter (owner = request.user) .values_list ( ' the above mentioned id ' , ' name ')
 9 
10     def queryset(self, request, queryset):
11         category_id = self.value()
12         if category_id:
13             return queryset.filter(category_id=self.value())
14         return queryset
CategoryOwnerFilter

parameter_name parameter is the name of the URL in the query,

Our filters can be filtered through this parameter.

lookups are we on the content of the page and use the query id

For example, when I click on Django classification DOCTOR, then will call queryset method, self.value () is used in the query id we set lookups, where my classification id is 3, passed in parameter is 3.

After the coding is completed, remember to change the filter in PostAdmin

. 1 list_filter = [CategoryOwnerFilter]   # page Filters

 

 

Custom list page data

 

Next, we want users to log in only to see the list page article that you have created.

We need to rewrite get_queryset method (I did not find this one explained in the document, but in any case, to see its name we know it returns a QuerySet object, then we can use the filter to filter! This you can achieve the effect we want.)

1 def get_queryset(self, request):
2     qs = super(PostAdmin, self).get_queryset(request)
3     return qs.filter(owner=request.user)

 

 

 

Next, configure the edit page.

First we have to define what things can be customized in the edit page, such as:

l button positions

l which fields need to be filled user, which do not even have to fill display

L field shows the order page is not be adjusted, whether the placement can be adjusted

l input box style

Position of the button with save_on_top to control whether to show the button at the top of the page

And a display field showing whether the order, or may be configured by fields fieldsets

1 fields = (
2     ('category', 'title'),
3     'desc',
4     'status',
5     'content',
6     'tag',
7 )

Try the trial fieldsets replace fields:

Fieldsets format requirements list tuple has two elements, e.g.

. 1 fieldsets = (
 2      (name, {contents}),
 3      (name, {contents}),
 4 )

After modification fieldsets;

. 1 fieldsets = (
 2      ( ' base configuration ' , {
 . 3          ' Description ' : ' Basic Configuration description ' ,
 . 4          ' Fields ' : (
 . 5              ( ' title ' , ' category ' ),
 . 6              ' Status ' ,
 . 7          ),
 . 8      } ),
 9      ( ' content ' , {
 10          'description': ' Abstract default selected content before the 140 words ' ,
 . 11          ' Fields ' : (
 12 is              ' desc ' ,
 13 is              ' Content ' ,
 14          ),
 15      }),
 16      ( ' additional information ' , {
 . 17          ' classes ' : ( ' Collapse ' ,),
 18 is          ' Fields ' : ( ' Tag ' ,),
 . 19      }),
 20 is )
fieldsets

 

Results page:

The configuration fields just like the original fields effect is the same.

The role of classes is to be configured to the plate with some CSS properties, Django admin default support collapse and wide.

description clearly described plate.

 

Custom static resources into

Django provides us with an interface to add css and js:

1 class Media:
2     css = {
3     'all': ("https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css", ),
4 }
5 js = ("https://cdn.bootcss.com/twitter-bootstrap/4.4.0/js/bootstrap.bundle.js", )

 

 

Custom Form

The above configuration is based on ModelAdmin if we have more customization needs, you should use ModelForm, create a new file in blogApp adminforms.py, we want to show desc customize this field can be written

1 from django import forms
2 
3 
4 class PostAdminForm(forms.ModelForm):
5     desc = forms.CharField(widget=forms.Textarea, label='摘要', required=False)

Configuration in the PostAdmin

1 form = Post Admin Form

We can see a summary of the components have changed Textarea

 

 

 

Custom site

We customize the site can be achieved by a system of external logic to provide multiple sets of admin background. In our original pages, articles classification data management and user management are together, so actually looked quite awkward for the functional point also inappropriate, so we have to separate them.

admin.site module django provided we previously used, and there's actually a site django.contrib.admin.AdminSite example, let's define your own site through inheritance, the code is as follows:

 1 from django.contrib.admin import AdminSite
 2 
 3 
 4 class CustomSite(AdminSite):
 5     site_title = 'Blog管理后台'
 6     site_header = 'Blog'
 7     index_title = '首页'
 8 
 9 
10 custom_site = CustomSite(name='cus_admin')

New Blog directory under a custom_site.py file, the code posted up, then modify all the admin App in the register.

1 @admin.register(Category, site=custom_site)

In our PostAdmin, we have a custom operator method, because the site was changed to a custom module module, so the name of the reverse is also need to be modified

1 def operator(self, obj):
2     return format_html(
3         '<a href="{}">编辑</a>',
4         reverse('cus_admin:blogApp_post_change', args=(obj.id,))
5     )

Finally, add routes in urls.py

1 urlpatterns = [
2     path('admin/', custom_site.urls),
3     path('super_admin/', admin.site.urls),
4 ]

 

This will have two sets of back-end address. Note that the two systems are based on a logic of the user's system, but we were divided on the URL.

 

Extracting the base class Admin

In our admin, we rewrite the save_model method and get_queryset method, which let us have a lot of code duplication, poor quality, so that the code can be made simple through inheritance.

Abstract base class BaseOwnerAdmin

 1 from django.contrib import admin
 2 
 3 
 4 class BaseOwnerAdmin(admin.ModelAdmin):
 5     exclude = ('owner', )
 6 
 7     def get_queryset(self, request):
 8         qs = super(BaseOwnerAdmin, self).get_queryset(request)
 9         return qs.filter(owner=request.user)
10 
11     def save_model(self, request, obj, form, change):
12         obj.owner = request.user
13         return super(BaseOwnerAdmin, self).save_model(request, obj, form, change)

Put it base_admin.py files under Blog directory of the App admin Inheritance can be changed BaseOwnerAdmin.

 

Finally, there is added View operation log.

1 @admin.register(LogEntry, site=custom_site)
2 class LogEntryAdmin(admin.ModelAdmin):
3     list_display = ['object_repr', 'object_id', 'action_flag', 'user', 'change_message']

 

 

After starting the development of user-oriented interface.

Guess you like

Origin www.cnblogs.com/ylnx-tl/p/12613263.html