已复制
全屏展示
复制代码

使用Django开发简单的后台管理

· 2 min read

配置Django

假设我现在要创建一个项目 panda_admin


pip install mysqlclient Django

# 创建项目(不需要提前创建任何目录)
django-admin startproject panda_admin

# 创建你的应用,假如应用名称为:camera
python manage.py startapp camera


# 目录结构如下:ls -l
drwxr-xr-x  9 yzy  staff  288 Oct 11 23:05 camera
-rwxr-xr-x  1 yzy  staff  667 Oct 11 18:18 manage.py
drwxr-xr-x  8 yzy  staff  256 Oct 11 22:53 panda_admin

# 目录结构如下:tree
.
├── camera
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
├── panda_admin
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py


# 修改连接MySQL的配置: panda_admin/settings.py
DATABASES = {
    'default': {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "mytable",
        "USER": "root",
        "PASSWORD": "xxx",
        "HOST": "127.0.0.1",
        "PORT": "3306",
    }
}

# 增加创建的应用:camera.apps.CameraConfig
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "camera.apps.CameraConfig",
]

# 这样设置了以后:不管是页面还是数据库都使用系统时区了
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True  # 页面展示时间格式
USE_TZ = False

  • 创建需要管理的model文件 camera/models.py
from django.db import models

class User(models.Model):
    id = models.IntegerField(primary_key=True)
    phone = models.CharField(max_length=200)
    username = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'users'

  • 把创建的model注册到admin中
from django.contrib import admin

from camera.models import User


class UserAdmin(admin.ModelAdmin):
    # 展示的字段
    list_display = ('id', 'phone', 'username', 'created_at', 'updated_at')
    list_display_links = ('id',)
    search_fields = ('phone', 'username')
    list_filter = ('id', 'phone', 'username')


# Register your models here.
admin.site.site_header = 'PANDA后台管理'
admin.site.site_title = 'PANDA后台管理'

admin.site.register(User, UserAdmin)


# 创建admin管理需要的数据表
python manage.py migrate

# 会创建Django的管理表
+----------------------------+
| Tables_in_mytable          |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)



# 创建登录用户
python manage.py createsuperuser

# 启动
python manage.py runserver

# 访问 8000 端口,使用刚刚创建的用户登录

上线时设置 DEBUG = False,然后配置 nginx


server {
    listen 8979;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8000;
    }

    location /static {
        alias /Users/yzy/programs/panda_admin/static;
    }
}

🔗

文章推荐