We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The outer world you see is a reflection of your inner self.
Creating model managers
managers
objects
我们也可以自定义 managers,这里自定义一个 manager 用于检索所有公开状态的 posts
manager
posts
两种自定义方法:
models.py
class PublishedManager(models.Manager): def get_queryset(self): return super(PublishedManager, self).get_queryset().filter(status='published') class Post(models.Model): # ... objects = models.Manager() published = PublishedManager()
Creating list and detail views
创建一个视图用于显示 posts 列表
views.py
from django.shortcuts import render, get_object_or _404 from .models import Post def post_list(request): posts = Post.published.all() return render(request, 'blog/post/list.html', {'posts': posts})
post_list()
request
HttpResponse
创建一个视图用于显示单一的 post 内容
post
def post_detail(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish_year=year, publish_month=month, publish_day=day) return render(request, 'blog/post/detail.html', {'post': post})
year
month
day
slug
date
Adding URL patterns for your views
URL patterns
blog
urls.py
from django.conf.urls import url from . import views urlpatterns = [ # post views url(r'^$', views.post_list, name='post_list'), url(r'^(?P<year>\d{4})/(?P<month>\d{2}/(?P<day>\d{2})/(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'), ]
URL pattern
post_list
post_detail
app
mysite
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')), ]
Canonical URLs for models
URL
Post
Django
get_absolute_url()
reverse()
URLs
from django.core.urlresolvers import reverse Class Post(models.Model): # ... def get_absolute_url(self): return reverse('blog:post_detail', args=[self.publish.year, self.publish.strftime('%m'), self.publish.strftime('%d'), self.slug])
strftime()
Creating templates for your views
html
templates
templates/ blog/ base.html post/ list.html detail.html
base.html
HTML
list.html
detail.html
post list
post detail
{% tag %}
{{ variable }}
{{ variable|filter }}
{% load staticfiles %} <!DOCTYPE html> <html> <head> <title>{% block title %}{% endblock %}</title> <link href="{% static "css/blog.css" %}" rel="stylesheet"> </head> <body> <div id="content"> {% block content %} {% endblock %} </div> <div id="sidebar"> <h2>My blog</h2> <p>This is my blog.</p> </div> </body> </html>
{% load staticfiles %}
django.contrib.staticfiles
{% static %}
manage.py
static
setting.py
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), )
{% block %}
block
title
content
普通继承
super继承
新式类
object
经典类
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Creating model managers
managers
是objects
,objects
可以检索所有数据库中的对象数据我们也可以自定义
managers
,这里自定义一个manager
用于检索所有公开状态的posts
两种自定义方法:
managers
创建一个新的managers
models.py
,添加managers
中添加新的方法Creating list and detail views
创建一个视图用于显示
posts
列表views.py
,添加post_list()
中有一个参数request
, 所有视图都需要这个参数HttpResponse
对象创建一个视图用于显示单一的
post
内容views.py
,添加year
month
day
post
slug
date
可以获得单一的post
Adding URL patterns for your views
URL patterns
用正则表示blog
目录下创建urls.py
文件URL pattern
没带任何参数,映射的就是post_list
视图URL pattern
带了4个参数,映射的就是post_detail
视图app
的最好方法就是为每个app
创建urls.py
blog
这个应用的URL patterns
代入到项目的URL patterns
中mysite
文件夹下的urls.py
Canonical URLs for models
post_detail
中的URL
来规范Post
中的URL
Django
中规范的方法就是 添加一个get_absolute_url()
方法到模型中get_absolute_url()
方法中用reverse()
方法来创建URLs
models.py
strftime()
方法,前面可以有0的存在(不会忽略数字之前的零)Creating templates for your views
html
模板来显示posts
blog
文件夹下创建templates
,如下逐层创建base.html
包括了网站HTML
的主要结构,将内容划分为主要内容区域和侧边栏区域list.html
和detail.html
这两个文件则继承了base.html
分别显示post list
和post detail
Django
中的模板语言:{% tag %}
{{ variable }}
{{ variable|filter }}
base.html
:{% load staticfiles %}
: 告诉Django
导入静态文件中的模板标签(由django.contrib.staticfiles
提供),导入之后,就可以在这个模板中使用{% static %}
manage.py
同级目录中创建static
文件夹,则需要在setting.py
中添加:STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), )
{% block %}
: 在此处定义了一个block
,上面定义了两个,一个命名为title
,另一个是content
0x02 Python
普通继承
和super继承
super继承
机制中可以保证公共父类仅被执行一次super继承
只能用于新式类,用于经典类会报错新式类
: 必须有继承的类,如果没有想继承的,那就继承object
,多继承的时候,采用广度优先的查找算法经典类
: 可以没有父类,多继承的时候,采用深度优先的查找算法The text was updated successfully, but these errors were encountered: