-
How to Serve QR Images from Memory with Django
TL; DR: I used SpooledTemporaryFile with FileResponse to server generated QR images directly from memory, no disk IO involved. Please see my code sample with notes: 🙂
-
Django DB Migration Job with ArgoCD
A Job in Kubernetes is a one-off and immutable task to be carried out during deployment. But what if a job needs to run for each deployment? A new job with the same name can’t be deployed on top of the existing one, given it in completed or failed state. Since Kubernetes 1.23, A TTL(Time…
-
Internal Service in Kubernetes Cluster
In Kubernetes(K8s) cluster, 1 or more containers form a pod and every container in the pod can access other container’s port just like apps in the same local host. For example: – pod1 – nginx1 – gunicorn1, port:8000 – pod2 – nginx2 – gunicorn2, port:8000 So nginx1 can access gunicorn1’s port using localhost:8000 and nginx2…
-
Python+Django笔记之三
关于urls.py 假设test项目下有一个应用模块notes,那么: test/urls.py: from django.conf.urls.defaults import * urlpatterns = patterns(‘ ‘, (r’^notes/’, include(‘notes.urls’)), … ) 这样所有匹配/notes/*的访问将转交给notes/urls.py处理。 test/notes/urls.py: … urlpatterns = patterns(‘ ‘, url(r’^list/$’, views.list_notes, name=’note_list’), url(r’^(?P<note_id>\d+)/$’, views.show_note, name=’note_show’), ) 首先,由于需要反向解析(页面名称->URL),所以要使用url(),而不是原来的tuple。对应url(r’^list/$’, views.list_notes, name=’note_list’), 将匹配/notes/list/的访问指向给views.py中的list_notes()方法,并将’note_list’名称反向解析到/notes/list/的URL,以便在webapp中生成链接。 第二行中的(?P<note_id>\d+)是一个参数定义,其中note_id是参数名,\d+说明此参数是一个数字。其实起到的作用就是将/note/123/这样的url解释成一个调用show_note(request, note_id=123)。如果参数不是数字而是字词,那么\d+应该改为\w+,具体定义可以参考regex规则。
-
Python+Django笔记之二
一个简单的select * 操作的Django方式: 首先配置urls.py from django.conf.urls.defaults import * from misc import views urlpatterns = patterns(‘ ‘, (r’^notes/$’, views.list_notes), ) 其中r’^notes/$’是regex,用于匹配url;views.list_notes是views.py中的list_notes 方法。 然后写list_notes: def list_notes(request): notes = Note.objects.order_by(‘-created_date’) conduit = { ‘notes’: notes } return render_to_response( ‘misc/list.html’, conduit, context_instance = RequestContext(request), ) 其中conduit这个词来自Mass Effect 🙂 最后就是把上面的misc/list.html写出来: {% extends “base.html” %} {% block title %}List Notes –…
-
Python+Django笔记之一
副标题:PGB: Project Galactica Board 启动 从无到有启动一个Django project(Django发音类似Jungle),以pgb为例: >django-admin.py startproject pgb 设置数据库连接: >vim pgb/settings.py DATABASE_ENGINE = ‘mysql’ # ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’. DATABASE_NAME = ‘pgb’ # Or path to database file if using sqlite3. DATABASE_USER = ‘root’ # Not used with sqlite3. DATABASE_PASSWORD = ‘xxxxxx’ # Not used with sqlite3. 建立一个app(app可以看作项目的一个模块,一个app是一系列相关功能的集合,而且可以被多个project共用): >cd pgb >python…