livereload.js 嵌入到 django 的错误页面

寻找django框架的根目录: ipython In [1]: import django In [2]: django 找到django框架的根目录,进入它,比如: cd /usr/local/lib/python3.11/site-packages/django 从错误页面找到一段特征代码,然后搜索它寻找错误页面模板: ag 'seeing this error because you have' 找到后: 编辑500页面 vi views/templates/technical_500.html 在</body>前加入如下代码: <script> ...
2023-07-30 00:58 | 阅读 708 次 | 评论 0 条

django用live server、liveReload等工具无法实时刷新的解决办法

问题描述: liveReload 等工具可以在修改文件后实时刷新页面,但在django开发中,修改模板后有时候自动刷新无效,页面还是被缓存了,无论怎么设置debug模式都白搭,django的模板系统就算在开发环境还是有零点几秒的缓存的,按下面的步骤可以彻底关闭它: 1、打开django模块的 template/loaders/cached.py 文件,比如 vi /usr/local/lib/python3.11/site-packages/django/template/loaders/cached.py 2、找到 get_template 方法,在方法的最前面加入一行: ...
2023-07-26 06:43 | 阅读 702 次 | 评论 0 条

django显示字段choices枚举值的label的方法

使用 get_FOO_display 方法 详见:https://doc.bccnsoft.com/docs/django-docs-4.0-en/ref/models/instances.html#django.db.models.Model.get_FOO_display
2023-06-29 04:21 | 阅读 695 次 | 评论 0 条

django4.1.3及以上的版本提示“django.db.utils.NotSupportedError: MySQL 5.7 or later is required”的解决办法

网站升级到 django4.1.3 后,连接老版本的数据库(mysql5.5)的时候,时不时出现错误: File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 207, in check_database_version_supported raise NotSupportedError( django.db.utils.NotSupportedError: MySQL 5.7 or later is required (found 5.5.53). 解决...
2022-11-17 03:15 | 阅读 9400 次 | 评论 11 条

django models.FileField 保存文件的几种方式

假设是一个model,名为Attach class Attach(models.Model): file = FileField(upload_to="files/%Y/%m/%d") 保存上传文件,这个最简单: def upload(request): attach = Attach() attach.file = request.FILES.get('file') attach.save() 保存网络上采集的文件: import requests from django.core.files.base import Cont...
2022-11-07 18:15 | 阅读 1035 次 | 评论 0 条

Python中等同于php中urlencode的函数

是: urllib.parse.quote 而不是 urllib.parse.urlencode
2022-10-31 22:24 | 阅读 907 次 | 评论 0 条

关于python获取文件名后缀的方法

标准的方法是: >>> os.path.splitext('thefile.jpg')[1] '.jpg' 这样获得的是带点号的后坠,如果不要点号呢?当然可以在结果上继续处理 >>> os.path.splitext('thefile.jpg')[1][1:] 'jpg' 不过有更简便的方法,使用字符串的split方法 >>> 'thefile.jpg'.split('.')[-1] 'jpg'
2022-05-23 22:23 | 阅读 1326 次 | 评论 0 条

给 python 加上 end

有强迫症,看见python没有end关键字,就像将倾的大厦,向一边倾斜,缺少对称之美。于是总想着给python加个end。在十几年前听说韩国有程序员给python(整容😁)用注释的方式加end,比如: if True: print("hello") #end 这个思路不错,但是注释前面的#号看着还是不舒服。 又想到用定义一个end变量: if True: print("hello") end = '' 让end等于空字符串,这样前面不用带一个#号了,但后面拖着个小尾巴,还是不舒服。 既然注释和变量都不完美,那么用...
2022-05-23 16:28 | 阅读 1029 次 | 评论 1 条

python正则re.sub“替换大文件内容”的坑

可能是由于内存限制,对大文件只能替换前面的一部分,可以使用re.compile突破这种限制 比如: re.sub("abc", "123", largeText, re.S|re.I) 可以改成: match = re.compile("abc", re.S|re.I) match.sub("123", largeText)
2022-05-03 17:38 | 阅读 1244 次 | 评论 0 条

vscode开发python时auto import无效的解决办法

在settings.json加入如下配置: "python.analysis.indexing": true
2021-11-10 13:40 | 阅读 1250 次 | 评论 0 条

django在生产环境关闭日志的方法

在uwsgi.xml加入下面一项 <disable-logging>true</disable-logging>
2021-10-18 21:44 | 阅读 1084 次 | 评论 0 条

Django通过signal(信号)实现对所有的model(模型)删除记录时自动删除FileField、ImageField字段的文件

假设startapp创建的app为app1 在app1中创建一个helpers模块,加入如下代码: from django.db import models from django.db.models.signals import pre_delete from django.dispatch import receiver def bind_delete_signal(model): @receiver(pre_delete, sender=model) def pre_model_delete(sender, **kwargs):...
2021-07-07 17:15 | 阅读 1062 次 | 评论 0 条

正则表达式之零宽断言

有时候在使用正则表达式做匹配的时候,我们希望匹配一个字符串,这个字符串的前面或后面需要是特定的内容,但我们又不想要前面或后面的这个特定的内容,(这里的特定内容是指字符串,如果是字符可以用^排除),这时候就需要零宽断言的帮助了。所谓零宽断言,简单来说就是匹配一个位置,这个位置满足某个正则,但是不纳入匹配结果的,所以叫“零宽”,而且这个位置的前面或后面需要满足某种正则。 正预测先行断言 断言自身出现的位置的后面能匹配表达式exp 语法格式 (?=exp) ...
2021-03-28 02:47 | 阅读 886 次 | 评论 0 条

清理未发布的新闻,同时删除附件

bccn_shell进入docker,按向上键一次就会出现 ./manage.py shell ,如果不出现继续按。出现后按回车进入控制台。 from news.models import Article for article in Article.objects.filter(public=False).order_by('id')[0:1000]: article.delete() 一次删1000篇
2019-07-07 00:02 | 阅读 1304 次 | 评论 0 条

Django这个框架比它使用的语言python稳多了

Django诞生于2003年,到现在16年了,一直在稳妥的推进,没飚版本号,在rails如日中天的时候没借鉴rails,一直坚持自己的风格,向前兼容也做得很好。这点比python好,python从2到3的变动好像失去了方向。
2019-01-22 19:09 | 阅读 1702 次 | 评论 0 条

切记,python使用pickle.dumps的时候一定先把unicode给encode('utf-8')成str

pickle.dumps(unicode_draft) #不建议 pickle.dumps(unicode_draft.encode('utf-8')) #建议 直接pickle.dumps(unicode_draft)的时候,保存的是这种形式: V<ul>\u5982\u4f55\u89e3\u6790 pickle.dumps(utf8_str_draft)的时候,保存的是这种形式: S'\xe5\xad\x99\xe9\x91\xa' 一个V,一个S,S后面跟的是单引号包裹的字符串。 -------------------------...
2016-10-30 03:21 | 阅读 10224 次 | 评论 0 条

用apt-get安装uwsgi的问题

有时候用pip 安装 uwsgi因为各种依赖问题装不上,可以用apt-get来装。 apt-get install uwsgi apt-get install uwsgi-plugin-python 不过uwsgi.xml需要加一项 <plugins>python</plugins>
2015-12-21 16:18 | 阅读 2699 次 | 评论 0 条

给django的migrations瘦身

当migrations越来越多的时候执行 makemigrations 和 migrate 就会越来越慢,可以考虑对其瘦身(减少migrations文件的数量),有两种方法: 1、squashmigrations(官方推荐) 此方法将一个app中的多个migration文件合并为一个,详见 http://doc.bccnsoft.com/docs/django-docs-1.7-en/topics/migrations.html#squashing-migrations 2、手动删除migrations文件 步骤: ...
2015-12-06 19:30 | 阅读 6184 次 | 评论 0 条

记django的一个坑:django.db.migrations.loader.BadMigrationError: Migration 0001_squashed_0002_remove_content_type_name in app contenttypes has no Migration class

今天执行djangod的合并迁移的时候运行了: ./manage.py squashmigrations contenttypes 0002 然后再执行迁移命令就出现错误提示: django.db.migrations.loader.BadMigrationError: Migration 0001_squashed_0002_remove_content_type_name in app contenttypes has no Migration class 然后删除了网站所有migrations目录下的文件,清空了django_...
2015-12-06 19:07 | 阅读 4479 次 | 评论 0 条

django-debug-toolbar只对某个用户(管理员)显示的设置方法

网上的一些文章都是介绍只对某个IP显示的,下面的设置是IP无关的(用户账号有关) 1、安装 sudo pip install django-debug-toolbar 2、在 settings.py 的 MIDDLEWARE_CLASSES 项中加入 'debug_toolbar.middleware.DebugToolbarMiddleware', 3、在 settings.py 中加入 def custom_show_toolbar(reques...
2015-11-30 02:37 | 阅读 3770 次 | 评论 0 条
浏览2776932次
文章归档