一. 版本说明:
- python 3.7
- Django 2.2.7
- xadmin 2
二. 环境搭建
-
安装python3
sudo apt install python3.7
sudo apt install python3-pip -
创建虚拟环境
pip3 install virtualenv
virtualenv -p /usr/bin/python3.7 venv -
进入虚拟环境
cd venv
source ./bin/activate -
安装模块
pip install django==2.2.7
pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
sudo apt-get install libpython3.7-dev
三. Django命令
- django-admin startproject test_project(创建项目,test_project3项目名称)
- cd test_project
- python manage.py startapp your_app (创建你的APP)
- python manage.py makemigrations
- python manage.py migrate
- python manage.py createsuperuser (创建超级用户)
- python manage.py runserver (运行项目)
四. 修改后台管理使 xadmin 生效
- 修改 urls.py
import xadmin
xadmin.autodiscover()
urlpatterns = [
path('admin/', xadmin.site.urls),
]
- settings.py
INSTALLED_APPS中增加
'xadmin',
'crispy_forms',
'reversion',
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin', # 新增
'crispy_forms', # 新增
'reversion', # 新增
]
- admin.py中使用xadmin
from your_app.models import Customer
import xadmin
class CustomerAdmin(object):
list_display = ['id', 'name', 'address', 'age', 'birthday', 'hobby', 'remark', 'create_time', 'update_time']
ordering = ['-id']
list_filter = ('name', 'age', 'remark')
search_fields = ['name']
list_per_page = 30
xadmin.site.register(Customer, CustomerAdmin)
网友评论