基于django快速开发一个网站(二)

基于django快速开发一个网站(二)

* 使用xadmin+django构建一个后台

安装

我的系统基于django==2.0.0开发的,所以直接从源码安装

$ pip install git+git://github.com/sshwsfc/xadmin.git@django2

配置

在 settings.py 中注册相应库

INSTALLED_APPS = [
    ...
    'xadmin',
    'crispy_forms',
    'reversion',
]

添加 url 规则

from django.urls import path
import xadmin
xadmin.autodiscover()
# version模块自动注册需要版本控制的 Model
from xadmin.plugins import xversion
xversion.register_models()

urlpatterns = [
    # 将原先的 admin.site.urls 替换为以下
    path('admin/', xadmin.site.urls),
]

登陆

╰$ python3 manage.py runserver 127.0.0.1:8001

登录 127.0.0.1:8001/admin

 

 简单定制

注册 models

# 数据模型
# users/models.py
from django.db import models

# Create your models here.
class User(models.Model):
    nickname = models.CharField(max_length=50, blank=True)
    email = models.EmailField(('email address'), blank=True, unique=True)

    def __str__(self):
        return self.nickname
```

```python
# users/admin.py
import xadmin
from .models import User

# Register your models here.
class UserAdmin(object):
    # 在列表中显示 nickname email 两个字段
    list_display = ['nickname', 'email']

xadmin.site.register(User, UserAdmin)

修改左侧菜单栏中栏目

# users/apps.py

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'users'
    # verbose_name 为菜单栏目名字
    verbose_name = '用户'
```
```python
# users/__init__.py
default_app_config='users.apps.UsersConfig'

* 修改页面 title footer header

# users/admin.py
import xadmin
from .models import User
from xadmin import views

# Register your models here.
class UserAdmin(object):
    list_display = ['nickname', 'email']

class GlobalSetting(object):
    site_title = '雨夜的博客'
    site_header = '雨夜的博客'
    site_footer = '雨夜的博客'

xadmin.site.register(User, UserAdmin)
xadmin.site.register(views.CommAdminView, GlobalSetting)

* 添加搜索、过滤器

 # users/admin.py

class UserAdmin(object):
    list_display = ['nickname', 'email']
    # 根据 nickname 字段 搜索
    search_fields = ['nickname']
    list_filter = ['nickname']

 

posted @ 2020-09-07 19:08  雨夜的博客  阅读(505)  评论(0编辑  收藏  举报