Django相关

执行测试用例相关的指令:
python manage.py test:执行所有的测试用例
python manage.py test app_name, 执行该 app 的所有测试用例
python manage.py test app_name.case_name: 执行指定的测试用例

PS: 一些测试工具:unittest 或者 pytest

simpleui-setting.py 的编写

#!/usr/bin/python
# -*- coding:utf8 -*-
# 更改默认语言为中文
LANGUAGE_CODE = 'zh-hans'
# 去掉默认Logo或换成自己Logo链接
SIMPLEUI_LOGO = 'https://img-home.csdnimg.cn/images/20221207093454.jpg'

# 隐藏右侧SimpleUI广告链接和使用分析
SIMPLEUI_HOME_INFO = False
SIMPLEUI_ANALYSIS = False

# 隐藏首页的快捷操作和最近动作
SIMPLEUI_HOME_QUICK = False
SIMPLEUI_HOME_ACTION = False

# 修改左侧菜单首页设置
SIMPLEUI_HOME_PAGE = 'https://www.baidu.com'  # 指向页面
# SIMPLEUI_HOME_TITLE = 'google!'  # 首页标题
# SIMPLEUI_HOME_ICON = 'fa fa-code'  # 首页图标

# 设置右上角Home图标跳转链接,会以另外一个窗口打开
SIMPLEUI_INDEX = 'https://www.baidu.com'

#,可以支持离线
SIMPLEUI_STATIC_OFFLINE = True # 打开离线模式

django-simpleui的使用
 # 安装
 pip install django-simpleui
# 使用,simpleui必须在admin之前引入
INSTALLED_APPS = [
       'simpleui', # 注意这里
       'django.contrib.admin',
       'django.contrib.auth',
       'django.contrib.contenttypes',
       'django.contrib.sessions',
       'django.contrib.messages',
       'django.contrib.staticfiles',
       ...     
 ]
 

参考连接:
https://blog.csdn.net/weixin_42134789/article/details/116772878
官方文档:
https://simpleui.72wo.com/docs/simpleui/QUICK.html#%E4%BE%8B%E5%AD%90-2

使用xadmin时,显示app的中文名
编辑app文件加中的apps.py文件
#!/usr/bin/python
# -*- coding:utf8 -*-

from django.apps import AppConfig


class ProductConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'apps.product'
    verbose_name = '产品详情' # 添加这个参数,和mode.py 类似
    
DRF扩展xadmin的使用:
安装:
pip install xadmin 
若pip 安装失败 则,进入https://pypi.org/project/xadmin2/ 下载zip文件到本地,然后 pip install 路径.zip
ps:xadmin2 2.04 版本的才兼容django最新的版本。
import xadmin
from xadmin import views

from apps.login.models import User


class BaseSetting(object):
    # """xadmin的基本配置"""
    enable_themes = True  # 开启主题切换功能
    use_bootswatch = True


class GlobalSettings(object):
    # """xadmin的全局配置"""
    site_title = "codehub"  # 设置站点标题
    site_footer = "codehub"  # 设置站点的页脚
    menu_style = "accordion"  # 设置菜单折叠
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)

class UserAdminx(object):
    list_display = ['id', 'username','phone']  # 显示
    search_fields = ['id', 'username','phone']  # 搜索
    # list_filter = ['is_delete', 'username']  # 过滤显示
    # list_editable = [] # 在列表页可以快速直接编辑的字段
    ordering = ['username'] # 默认排序的字段
    list_export = ('xls', 'json')  # list_export设置为None来禁用数据导出功能
    list_export_fields = ('id', 'username', 'phone')  # 允许导出的字段

    show_bookmarks = True # 控制是否显示书签功能
    show_detail_fields = ['phone']  # 显示详情
    list_per_page = 15  # 默认每页显示多少条记录,默认是100条

# xadmin限制,需要先解除模型注册,再重新注册视图
# xadmin.site.register(模型, 模型管理类)

xadmin.site.unregister(User)
xadmin.site.register(User, UserAdminx)

PS:因为是重写User类,User表已经存在,所以不用注册User表

使用参考链接:
https://zhuanlan.zhihu.com/p/298016309
https://zhuanlan.zhihu.com/p/152645429
https://blog.51cto.com/liangdongchang/3116442

forms模板

前提安装了 django-simple-captcha  库
from captcha.fields import CaptchaField
from django import forms
from . import models

class UserForm(forms.Form):

    username = forms.CharField(label='用户名',widget=forms.TextInput(
        attrs={"class": "form-control", "placeholder": "请输入用户名", "value": "", "required": "required", }),
                               max_length=50, error_messages={"required": "用户名不能为空", })

    password = forms.CharField(label='密码',widget=forms.PasswordInput(
        attrs={"class": "form-control", "placeholder": "请输入密码", "value": "", "required": "required", }),
                               min_length=2, max_length=50, error_messages={"required": "密码不能为空", })

    captcha = CaptchaField(label='验证码')

    def clean(self):
        # 验证码
        try:
            captcha_x = self.cleaned_data['captcha']
        except Exception as e:
            print('except: ' + str(e))
            return "验证码有误,请重新输入"
            # raise forms.ValidationError(u"验证码有误,请重新输入")
# views的代码
def login(request):
	hashkey = CaptchaStore.generate_key()
    imgage_url = captcha_image_url(hashkey)
    # locals表示上面的变量自动组件成字典,传递给html
    return render(request, 'login.html',locals())
## 前端代码
<!--//验证码动态刷新实现 -->
<div class="form-group">
                <input type="text" id="id_reg_captcha_1" name="captcha_1" class="form-control form-control-captcha fl"
                       placeholder="请输入验证码">
                <span class="v5-yzm fr">
                    <a href="#" class="next-captcha">
                    <img src="{{ imgage_url }}" class="captcha" alt="captcha">换一张</a>
                </span><input id="id_reg_captcha_0" name="captcha_0" type="hidden" value="{{ hashkey }}">

            </div>
# js代码
<script>
<!--//验证码动态刷新实现 -->
$('.captcha').click(function () {
    $.getJSON("/captcha/refresh/", function (result) {
    $('.captcha').attr('src', result['image_url']);
    $('#id_captcha_0').val(result['key'])
});
});


</script>

参考链接:
https://blog.csdn.net/xxm524/article/details/48370337
https://www.liujiangblog.com/course/django/110

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值