Django开发笔记一
1、运行
1 | python manage.py runserver 0.0 . 0.0 : 8000 |
2、添加app
在pycharm的工具栏找到 Tools,点击之后会有“Run manage.py task”选项进入终端
1 | startapp app_name |
app添加到文件夹apps下
新建apps文件夹,markas sourse root
settings.py中
1 2 3 4 5 | import os import sys # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert( 0 , os.path.join(BASE_DIR, 'apps' )) |
3、生成默认数据库
在2的终端中:
1 2 | makemigrations migrate |
makemigrations会记录修改的models.py,并且在migrations生成记录文件,此时数据库未作修改
migrate会把修改同步到数据库
4、添加 Django administration
1 | python manage.py createsuperuser<br><br>root root@qq.com rootroot<br> |
访问 http://127.0.0.1:8000/admin/ 进入
5、user继承AbstractUser
users app下models.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class UserProfile(AbstractUser): nickname = models.CharField(max_length = 50 , verbose_name = u "昵称" ) birthday = models.DateField(max_length = 50 , null = True , blank = True , verbose_name = u "生日" ) gender = models.CharField(choices = (( "male" , u "男" ), ( "female" , u "女" )), default = u "female" , max_length = 10 ) address = models.CharField(max_length = 100 , default = u"") mobile = models.CharField(max_length = 11 , null = True , blank = True ) image = models.ImageField(upload_to = "images/upload/user/%y/%m" , default = u "images/default/user_default.png" , max_length = 100 ) class Mate: verbose_name = u "用户信息" verbose_name_plural = verbose_name ordering = [ "-id" ] def __unicode__( self ): return self .username |
admin.py
1 2 3 4 5 6 7 8 9 10 11 | from django.contrib import admin # Register your models here. from users.models import UserProfile class UserProfileAdmin(admin.ModelAdmin): pass admin. |
settings.py
1 | AUTH_USER_MODEL = "users.UserProfile" |
修改之后发现后台访问报错: 'WSGIRequest' object has no attribute 'user'
修改
1 | MIDDLEWARE 改为 MIDDLEWARE_CLASSES |
6、xadmin使用
1 2 | pip install xadmin pip list |
settings.py中INSTALLED_APPS添加:
1 2 | 'xadmin' , 'crispy_forms' , |
url.py中:
1 2 3 4 5 | import xadmin urlpatterns = [ url(r '^xadmin/' , xadmin.site.urls), ] |
之前admin.py
1 2 3 4 5 6 7 8 9 10 11 | from django.contrib import admin # Register your models here. # from users.models import UserProfile # # # class UserProfileAdmin(admin.ModelAdmin): # pass # # admin.site.register(UserProfile,UserProfileAdmin) |
7、xadmin添加模块
在app下新建adminx.py代替admin.py
1 2 3 4 5 6 7 8 9 10 11 | # coding=utf-8 import xadmin from .models import EmailVerifyRecord class EmailVerifyRecordAdmin( object ): pass xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin) |
这样就把models.py中的模块添加进后台管理
1 2 3 4 5 6 7 8 9 10 11 12 13 | class EmailVerifyRecord(models.Model): code = models.CharField(max_length = 20 ,verbose_name = u "验证码" ) email = models.EmailField(max_length = 50 ,verbose_name = u "邮箱" ) send_type = models.CharField(verbose_name = u "发送类型" ,choices = (( "register" ,u "注册" ),( "forget" ,u "忘记密码" ),( "update_email" ,u "修改邮箱" )),max_length = 50 ) send_time = models.DateTimeField(verbose_name = u "发送时间" ,default = datetime.now) class Meta: verbose_name = u "邮箱验证码" verbose_name_plural = verbose_name def __unicode__( self ): return '{0}({1})' . format ( self .code, self .email) |
用户模块自动会加入,不需要添加
8、定义列表显示的字段
修改以上xadmin.py的内容
1 2 | class EmailVerifyRecordAdmin( object ): list_display = [ 'code' , 'email' , 'send_type' , 'send_time' ] |
9、定义搜索的字段
1 2 3 | class EmailVerifyRecordAdmin( object ): ... search_fields = [ 'code' , 'email' , 'send_type' , 'send_time' ] |
10、添加过滤器增强搜索
1 2 3 | class EmailVerifyRecordAdmin( object ): ... list_filter = [ 'code' , 'email' , 'send_type' , 'send_time' ] |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
· drools 规则引擎和 solon-flow 哪个好?solon-flow 简明教程