Django Muke network production

Book back: powerful Django + killer xadmin develop an online education website (newly developed) to build an online education website from scratch

the second day:

Today's task is to understand the focus of the whole course. xamin is a third-party background management system based on django

Foreplay part

The first is to reference the xadmin resources
Then configure step by step according to the tutorials inside. Such an error may be encountered in the process of configuration

ModuleNotFoundError: No module named 'widgets'

The online tutorial is to let you directly

pip install DjangoUedito

However, the version of DjangoUedito he downloaded is incompatible with Python 3, and other errors will still occur. The solution here is to search for DjangoUedito DjangoUeditor on github and put it into vevn

The package problem is solved, and the next step is the setting of setting.py
The first is the problem of Chinese display

LANGUAGE_CODE = 'zh-hans' 

TIME_ZONE = 'Asia/shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False # The setting is not a UTC time zone

Set the corresponding in setting to the above setting.

The next step is to set the path of background upload file

# setting.py
# The following operation is used to add pictures and external files
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname('__file__')))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# urls
from django.conf.urls.static import static
from Project name import settings
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

Another step is to create a media folder in the root directory of the project. This file is the file storage folder for future background upload.

Climax part

In django native admin, if we want to register a database, we need to register in admin.py (built in the new app)

In xadmin, the registration steps are almost the same as those in native. Only the file name cannot be used. Here, we need to create an adminx.py file in the corresponding app to register the database

The following is the adminx registered in this project

# courses
from apps.courses import models
import xadmin

class GlobalSettings(object):
    site_title='Screen science management system'
    site_footer = 'Screen science management system'
    menu_style='accordion'

xadmin.site.register(xadmin.views.CommAdminView,GlobalSettings)

class BaseSetting(object):
    enable_themes=True
    use_bootswatch =True
xadmin.site.register(xadmin.views.BaseAdminView,BaseSetting)

# The above two are to modify the style of xadmin. They can be put into any adminx.py
class CourseAdmin(object):
    list_display = ['name', 'teacher', 'desc', 'category', 'students', 'tag', 'degree']
    search_fields = ['name', 'teacher', 'desc', 'category', 'tag']
    list_filter = ['name', 'teacher__name', 'add_time', 'category', 'tag']
    list_editable = ['name', 'teacher', 'category', 'tag']


xadmin.site.register(models.Course, CourseAdmin)


class LessonAdmin(object):
    list_display = ['course', 'name', 'learn_times']
    search_fields = ['course', 'name']
    list_filter = ['course', 'name', 'add_time']
    list_editable = ['course', 'name']


xadmin.site.register(models.Lesson, LessonAdmin)


class VideoAdmin(object):
    list_display = ['lesson', 'name', 'learn_times']
    search_fields = ['lesson', 'name']
    list_filter = ['lesson', 'name', 'add_time']
    list_editable = ['lesson', 'name']


xadmin.site.register(models.Video, VideoAdmin)


class CourseResourceAdmin(object):
    list_display = ['course', 'name']
    search_fields = ['course', 'name']
    list_filter = ['course', 'name', 'add_time']


xadmin.site.register(models.CourseResource, CourseResourceAdmin)

# operation
from apps.operation import models
import xadmin


class CityAdmin(object):
    list_display=['name','desc']
    search_fields =['name','desc']
    list_filter = ['name','desc','add_time']
    list_editable=['name','desc']

xadmin.site.register(models.City, CityAdmin)


class CourseOrgAdmin(object):
    list_display = ['name', 'desc', 'tag', 'category', 'address', 'course_nums']
    search_fields = ['name', 'desc', 'address', 'categoryChoices']
    list_filter = ['name', 'desc', 'add_time', 'category']
    list_editable = ['name', 'desc', 'categoryChoices']


xadmin.site.register(models.CourseOrg, CourseOrgAdmin)


class TeacherAdmin(object):
    list_display = ['org','name', 'work_years', 'work_company', 'work_position']
    search_fields = ['name', 'work_company', 'org', 'work_position']
    list_filter = ['name', 'org', 'add_time', 'work_company']
    list_editable = ['org', 'name', 'work_company']


xadmin.site.register(models.Teacher, TeacherAdmin)

# organizations
import xadmin
from apps.organizations.models import *
class UserAskAdmin(object):
    list_display = ['name', 'mobile', 'course_name', 'add_time']
    search_fields = ['name', 'mobile', 'course_name']
    list_filter = ['name', 'mobile', 'course_name', 'add_time']




class UserCourseAdmin(object):
    list_display = ['user', 'course', 'add_time']
    search_fields = ['user', 'course']
    list_filter = ['user', 'course', 'add_time']




class UserMessageAdmin(object):
    list_display = ['user', 'message', 'add_time']
    search_fields = ['user', 'message' ]
    list_filter = ['user', 'message', 'add_time']


class CourseCommentsAdmin(object):
    list_display = ['user', 'course', 'comments', 'add_time']
    search_fields = ['user', 'course', 'comments']
    list_filter = ['user', 'course', 'comments', 'add_time']


class UserFavoriteAdmin(object):
    list_display = ['user', 'fav_id', 'fav_type', 'add_time']
    search_fields = ['user', 'fav_id', 'fav_type']
    list_filter = ['user', 'fav_id', 'fav_type', 'add_time']


xadmin.site.register(UserAsk, UserAskAdmin)

xadmin.site.register(UserCourse, UserCourseAdmin)
xadmin.site.register(UserMessage, UserMessageAdmin)
xadmin.site.register(CourseComments, CourseCommentsAdmin)
xadmin.site.register(UserFavorite, UserFavoriteAdmin)

Here, xadmin will automatically register the user table into the data, so we don't need to set a userAdmin

Here, we can customize the Admin in the background display and other operations. There are many methods and parameters. Here, I'll give you a list of basic parameters

class CityAdmin(object):
    list_display=['name','desc']# list_display the fields displayed on the back home page
    search_fields =['name','desc'] 	# search_fields fields that can be searched during background search
    list_filter = ['name','desc','add_time']# list_ Fields in filter
    list_editable=['name','desc']# list_editable is a field that can be modified directly on the home page

Note the fields listed above. If there is a foreign key field, we can use the foreign key name__ The fields of the foreign key table are used for query or operation

example

class CourseAdmin(object):
    list_display = ['name', 'teacher', 'desc', 'category', 'students', 'tag', 'degree']
    search_fields = ['name', 'teacher', 'desc', 'category', 'tag']
    list_filter = ['name', 'teacher__name', 'add_time', 'category', 'tag']
    list_editable = ['name', 'teacher', 'category', 'tag']

Tags: Database Python Django

Posted by findshorty on Tue, 21 Sep 2021 23:13:28 +0530