# mysite/news/admin.py
from django.contrib import admin
from . import models
admin.site.register(models.Article)
That's it.
URL scheme
# mysite/news/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<int:pk>/', views.article_detail),
]
For example, if a user requested the URL “/articles/2005/05/39323/”, Django would call the function news.views.article_detail(request, year=2005, month=5, pk=39323).
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello, World!</h1>
{% endif %}
context: current_app, g, request, session
Deferred Request Callbacks
from flask import request, after_this_request
@app.before_request
def detect_user_language():
language = request.cookies.get('user_lang')
if language is None:
language = guess_language_from_request()
# when the response exists, set a cookie with the language
@after_this_request
def remember_language(response):
response.set_cookie('user_lang', language)
return response
g.language = language
Flask's after_request hooks are indeed executed in the reversed order of their declaration. Conversely, before_request hooks are executed in the order of their declaration.
If after_request hooks are declared on a blueprint, those are executed in reversed declaration order, before global after_request hooks. If before_request hooks are declared on a blueprint, those are executed in declaration order, after global before_request hooks.
Django: database-driven Web app
start
env setting
one settings.py and local_settings.py in different env timezone issue:
Data Model Layer within
standard ORM, query syntax and mature migration tool lazy QuerySet object, only execute when evaluated, such as
len, slicing, bool, str, repr
dynamic admin interface: a whole house
That's it.
URL scheme
For example, if a user requested the URL “/articles/2005/05/39323/”, Django would call the function
news.views.article_detail(request, year=2005, month=5, pk=39323)
.That's it.
views
That's it.
templates
Flask: keep the core simple but extensible, so be micro
env setting
one config.py and different cfg file in different env: local.cfg, test.cfg, real.cfg
Data Model Layer without
extension: flask-sqlalchemy, flask-migration
另外,对于 session 需要特殊关照,最好主动 commit and close
admin without
extension: flask-admin
URL scheme
app.route decorator and blueprint
views
That's it
templates
context: current_app, g, request, session
Deferred Request Callbacks
That's it
before_request and after_request order
Reference from SO
Django vs Flask
Deploy: Nginx, Gunicorn, WSGI, Linux, Docker, K8s