笔者 Python 版本为 3.10.6,Django 版本为 4.2.2
本项目代码已开源,可到 GitHub仓库 中查看
本项目用到的 xadmin
、DjangoUeditor
文件夹笔者已经按照 Python 3.10版本进行了修改,点击 链接 即可下载。但如果你使用的 Python 版本较低,可能会因此出现一些 BUG,请到 问题汇总 查看,如果没有,请私聊笔者。
正文
修改 chapter1/settings.py
中的 INSTALLED_APPS
数组
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'book',
'goods',
'xadmin',
'crispy_forms',
'django.conf',
'DjangoUeditor',
]
重写 chapter1/urls.py
文件
import xadmin
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path, include
urlpatterns = [
path('users/', include('users.urls')),
path('book/', include('book.urls')),
path('admin/', admin.site.urls),
path('goods/', include('goods.urls')),
path('xadmin/', xadmin.site.urls),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
修改 goods/admin.py
文件
import xadmin
from xadmin import views
from .models import GoodsInfo
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
class GoodsInfoAdmin(object):
list_display = ('id', 'name', 'price', 'weight', 'isnew')
list_editable = ('price', 'weight')
list_filter = ['name', 'price']
search_fields = ('name',)
style_fields = {'details': 'ueditor'}
class GlobalSettings(object):
site_title = '商品后台管理系统'
site_footer = '版权归属@东营科技有限公司'
xadmin.site.register(GoodsInfo, GoodsInfoAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
修改 goods/models.py
from DjangoUeditor.models import UEditorField
from django.db import models
class GoodsInfo(models.Model):
name = models.CharField(max_length=30, verbose_name='商品名称')
price = models.FloatField(verbose_name='商品价格', default=20.0)
weight = models.IntegerField(verbose_name='商品重量', default=500)
image = models.ImageField(upload_to='upload/%Y/%m', verbose_name='商品图片', default='upload/default.jpg')
isnew = models.BooleanField(verbose_name='是否新品', default=False)
# details = models.TextField(verbose_name='商品详情', default='')
details = UEditorField(verbose_name='商品详情', default='', toolbars='full')
def __str__(self):
return self.name
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
访问 http://127.0.0.1:8000/xadmin/
问题汇总
汇总一些你可能会遇到的问题
cannot import name 'lookup_spawns_duplicates' from 'django.contrib.admin.utils'
将 lookup_spawns_duplicates
替换为 lookup_needs_distinct
No module named "crispy_forms"
pip install django-crispy-forms
标签:Xadmin,name,Python,django,models,xadmin,urls,import,Django
From: https://www.cnblogs.com/huanfei/p/17460772.html