已实现功能api(获取系统配置),auth(获取token、查询用户、添加用户)

This commit is contained in:
jayhgq 2024-08-18 22:10:26 +08:00
parent c48a50c1a7
commit 1057a8d5e5
12 changed files with 113 additions and 89 deletions

View File

@ -15,7 +15,6 @@ from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
@ -27,7 +26,6 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
@ -72,7 +70,6 @@ TEMPLATES = [
WSGI_APPLICATION = 'CMS_Django_Backend.wsgi.application' WSGI_APPLICATION = 'CMS_Django_Backend.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases # https://docs.djangoproject.com/en/5.1/ref/settings/#databases
@ -83,7 +80,6 @@ DATABASES = {
} }
} }
# Password validation # Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
@ -102,7 +98,6 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/ # https://docs.djangoproject.com/en/5.1/topics/i18n/
@ -114,7 +109,6 @@ USE_I18N = True
USE_TZ = True USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/ # https://docs.djangoproject.com/en/5.1/howto/static-files/
@ -134,3 +128,11 @@ SESSION_EXPIRE_AT_BROWSER_CLOSE = True
# 如果设置了True每次用户会话更新时会话cookie的过期时间都会被更新 # 如果设置了True每次用户会话更新时会话cookie的过期时间都会被更新
SESSION_SAVE_EVERY_REQUEST = False SESSION_SAVE_EVERY_REQUEST = False
import os
# 配置 MEDIA_ROOT 作为你上传文件在服务器中的基本路径
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload') # 注意此处不要写成列表或元组的形式
# 配置 MEDIA_URL 作为公用 URL指向上传文件的基本路径
MEDIA_URL = '/media/'

View File

@ -16,6 +16,8 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
# path('admin/', admin.site.urls), # path('admin/', admin.site.urls),
@ -23,4 +25,4 @@ urlpatterns = [
# path('auth', include('apps.auth.urls')) # path('auth', include('apps.auth.urls'))
path('api/', include('apps.api.urls')), path('api/', include('apps.api.urls')),
path('auth/', include('apps.auth.urls')) path('auth/', include('apps.auth.urls'))
] ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

View File

@ -1,4 +1,4 @@
# Generated by Django 5.1 on 2024-08-17 09:50 # Generated by Django 5.1 on 2024-08-18 14:07
from django.db import migrations, models from django.db import migrations, models
@ -24,5 +24,8 @@ class Migration(migrations.Migration):
('update_by', models.CharField(max_length=50, verbose_name='更新人')), ('update_by', models.CharField(max_length=50, verbose_name='更新人')),
('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')), ('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
], ],
options={
'verbose_name': '系统配置表',
},
), ),
] ]

View File

@ -1,17 +0,0 @@
# Generated by Django 5.1 on 2024-08-17 15:22
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='sysconfig',
options={'verbose_name': '系统配置表'},
),
]

View File

@ -1,5 +1,6 @@
# Generated by Django 5.1 on 2024-08-17 15:22 # Generated by Django 5.1 on 2024-08-18 14:07
import apps.auth.models
from django.db import migrations, models from django.db import migrations, models
@ -17,10 +18,10 @@ class Migration(migrations.Migration):
('id', models.AutoField(primary_key=True, serialize=False, unique=True, verbose_name='id')), ('id', models.AutoField(primary_key=True, serialize=False, unique=True, verbose_name='id')),
('username', models.CharField(blank=True, max_length=50, null=True, unique=True, verbose_name='用户名')), ('username', models.CharField(blank=True, max_length=50, null=True, unique=True, verbose_name='用户名')),
('pwd', models.CharField(blank=True, max_length=1000, null=True, verbose_name='密码')), ('pwd', models.CharField(blank=True, max_length=1000, null=True, verbose_name='密码')),
('email', models.CharField(blank=True, max_length=100, null=True, verbose_name='电子邮件')), ('email', models.EmailField(max_length=100, verbose_name='电子邮件')),
('phone', models.CharField(max_length=11, verbose_name='手机')), ('phone', models.CharField(blank=True, max_length=11, null=True, verbose_name='手机')),
('create_time', models.DateTimeField(verbose_name='注册时间')), ('create_time', models.DateTimeField(verbose_name='注册时间')),
('avatar', models.ImageField(upload_to='', verbose_name='头像')), ('avatar', models.ImageField(upload_to=apps.auth.models.user_directory_path, verbose_name='头像')),
('last_login_time', models.DateTimeField(auto_now=True, verbose_name='最后登录时间')), ('last_login_time', models.DateTimeField(auto_now=True, verbose_name='最后登录时间')),
], ],
options={ options={

View File

@ -1,4 +1,12 @@
from django.conf.global_settings import MEDIA_ROOT
from django.db import models from django.db import models
import os
def user_directory_path(instance, filename):
ext = filename.split('.').pop()
filename = '{0}{1}.{2}'.format(instance.username, instance.phone, ext)
return os.path.join("avatar", filename) # 系统路径分隔符差异,增强代码重用性
# Create your models here. # Create your models here.
@ -9,10 +17,10 @@ class User(models.Model):
id = models.AutoField(verbose_name="id", primary_key=True, unique=True) id = models.AutoField(verbose_name="id", primary_key=True, unique=True)
username = models.CharField(max_length=50, unique=True, verbose_name="用户名", null=True, blank=True) username = models.CharField(max_length=50, unique=True, verbose_name="用户名", null=True, blank=True)
pwd = models.CharField(max_length=1000, verbose_name="密码", null=True, blank=True) pwd = models.CharField(max_length=1000, verbose_name="密码", null=True, blank=True)
email = models.CharField(max_length=100, verbose_name="电子邮件") email = models.EmailField(max_length=100, verbose_name="电子邮件")
phone = models.CharField(max_length=11, verbose_name="手机", null=True, blank=True) phone = models.CharField(max_length=11, verbose_name="手机", null=True, blank=True)
create_time = models.DateTimeField(verbose_name="注册时间") create_time = models.DateTimeField(verbose_name="注册时间")
avatar = models.ImageField(verbose_name="头像") avatar = models.ImageField(verbose_name="头像", upload_to=user_directory_path)
last_login_time = models.DateTimeField(verbose_name="最后登录时间", auto_now=True) last_login_time = models.DateTimeField(verbose_name="最后登录时间", auto_now=True)
def __str__(self): def __str__(self):
@ -20,3 +28,10 @@ class User(models.Model):
class Meta: class Meta:
verbose_name = '用户表' verbose_name = '用户表'
# 这里定义一个方法,作用是当用户注册时没有上传照片,模板中调用 [ModelName].[ImageFieldName].url 时赋予一个默认路径
def avatar_url(self):
if self.avatar and hasattr(self.avatar, 'url'):
return self.avatar.url
else:
return '/media/avatar/default.jpg'

View File

@ -4,4 +4,5 @@ from apps.auth import views
urlpatterns = [ urlpatterns = [
path("gettoken/", views.gettoken, name="getToken"), path("gettoken/", views.gettoken, name="getToken"),
path("searchuser/", views.search_user, name="searchuser"), path("searchuser/", views.search_user, name="searchuser"),
path("adduser/", views.add_user, name="addUser"),
] ]

View File

@ -1,8 +1,9 @@
import json import json, datetime, base64
from django.shortcuts import HttpResponse from django.shortcuts import HttpResponse
from django.middleware.csrf import get_token from django.middleware.csrf import get_token
from django.views.decorators.http import require_GET, require_POST from django.views.decorators.http import require_GET, require_POST
from apps.auth import models as auth_models from apps.auth import models as auth_models
from django.contrib.auth.hashers import make_password, check_password
# Create your views here. # Create your views here.
@ -33,12 +34,28 @@ def search_user(request):
@require_POST @require_POST
def adduser(request): def add_user(request):
""" """
用户注册 用户注册
:param request: POST提交注册信息 :param request: POST提交注册信息
:return: 注册结果 :return: 注册结果
先查询数据库中用户名username是否存在如果存在则提示用户更改用户名若不存在则进行存储
""" """
username = request.POST.get("username")
pwd_base64 = base64.b64decode(request.POST.get("pwd"))
pwd = make_password(pwd_base64)
email = request.POST.get("email")
phone = request.POST.get("phone")
create_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
last_login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(username, pwd, email, phone)
avatar = request.FILES.get("avatar")
auth_models.User.objects.create(
username=username,
pwd=pwd,
email=email,
phone=phone,
create_time=create_time,
last_login_time=last_login_time,
avatar=avatar
)
return HttpResponse("添加用户成功") return HttpResponse("添加用户成功")

Binary file not shown.

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

BIN
upload/avatar/default.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB