一. 版本說明:
- python 3.7
- Django 2.2.7
- xadmin 2
二. 環(huán)境搭建
安裝python3
sudo apt install python3.7
sudo apt install python3-pip創(chuàng)建虛擬環(huán)境
pip3 install virtualenv
virtualenv -p /usr/bin/python3.7 venv進入虛擬環(huán)境
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(創(chuàng)建項目璧眠,test_project3項目名稱)
- cd test_project
- python manage.py startapp your_app (創(chuàng)建你的APP)
- python manage.py makemigrations
- python manage.py migrate
- python manage.py createsuperuser (創(chuàng)建超級用戶)
- 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)