包含标签 Django articles

Crazy Import

最近发现 Django settings 在使用的时候,容易出现不经过检查的问题。 因此,想要在 settings 的基础上严格约束一下,减少问题。

……

Continue reading

ORM 中的 Pattern

最近在做技术选型,想要引入一些新的开发语言,开发技术到技术团队。大众编程语言在 WEB 方面的稳定性,已经被人所熟知了。但是数据库版本控制方面,每个程序设计语言做的深度和稳定性都有所区别。数据库版本控制和 ORM 密切相关。而…

……

Continue reading

Django tricks

关于 session django.contrib.sessions.middleware | Django documentation | Django session 是使用 cookie 来进行判断的,通过 session_cookie_name 来提取,然后通过 SessionStore() 来保存。 因此,Django 也是通过 sessionid 来区分用户;(或者其他什么名称)。 使用 cache 作为全局变量 cache.set(‘foo’, ‘bar’) # another function cache.get(‘foo’) # get ‘bar’ 使用 cache , django-redis 是一个好方法。可以在使用 cache.lock变量,在应用层面加锁,来完成异步操作,还可以支持分布式应用,十分方便。……

Continue reading

Django 写个过滤器 filter

用 Django 快速开发一个项目,发现 Django 的模板本身不带有整除功能,因此自己手写一个。 这是目录结构。在 urls.py 添加 app 的名称,例如 app_name = ‘app’ ,然后在与models.py 同级的目录下,新建一个 python 包, templatetags 的,新建自己需要的包名即可。 app_extra.py: 这里因为需求比较简单,因此直接就赋值了 11. 应用到模板中: 随后即可看到结果。 此外,寻得两个比较好用 template 插件 实现的东西虽然不多,但是起码把我不想关心的逻辑封装起来了。。……

Continue reading

使用Docker时遇到的sitemap问题

这两天感觉博客访问量已经不能靠用嘴宣传了,于是决定做一下sitemap,但是发现django自带的sitemap生成器存在这样的问题:生成的sitemap是依照container的hostname来显示的。 为此我查询了一下其他人是怎么搞的,发现他们都把nginx和django放在一个容器下——但是如此构建不仅麻烦,似乎违背了docker的构建思路,在一个container里面放了多个进程。因此,我认为下面的方案是合理的。 解决方案 Enabling the sites framework To enable the sites framework, follow these steps: Add ‘django.contrib.sites’ to your INSTALLED_APPS setting. Define a SITE_ID setting: SITE_ID = 1 Run migrate. django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database. To set the correct name and domain for your project, you can use a data migration.……

Continue reading

Latest articles

Categories

Tags

Meta