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
Your attitude, not your aptitude, will determine your altitude.
Creating custom template filters
Django
{{variable|my_filter}}
{{variable|my_filter:"foo"}}
{{variable|filter1|filter2}}
markdown
Python Markdown
pip install Markdown==2.6.7
PyCharm
blog_tags.py
from django.utils.safestring import mark_safe import markdown @register.filter(name='markdown') def markdown_format(text): return mark_safe(markdown.markdown(text))
<,>,',"",&
HTML
XSS
markdown.markdown()
mark_safe()
Markdown
post/list.html
post/detail.html
{% load blog_tags %}
{{ post.body|linebreaks }} linebreaks 替换成 markdown {{ post.body|markdown }}
{{ post.body|truncatewords:30|linebreaks }} linebreaks 替换成 markdown {{ post.body|markdown|truncatewords_html:30 }}
truncatewords_html
Adding a sitemap to your site
sitemap
XML
django.contrib.sites
sites
site ID
SITE_ID = 1 INSTALLED_APPS = ( ... 'django.contrib.sites', 'django.contrib.sitemaps', )
python manage.py migrate
blog
sitemaps.py
from django.contrib.sitemaps import Sitemap from .models import Post class PostSitemap(Sitemap): changefreq = 'weekly' priority = 0.9 def items(self): return Post.published.all() def lastmod(self, obj): return obj.publish
sitemaps
Sitemap
changefreq
priority
post pages
items()
get_absolute_url()
post
URL
location()
lastmod()
sitemap URL
urls.py
from django.conf.urls import include, url from django.contrib import admin from django.contrib.sitemaps.views import sitemap from blog.sitemaps import PostSitemap sitemaps = { 'posts': PostSitemap, } urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')), url(r'sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'), ]
sitemap.xml
example.com
Site
http://localhost:8000/admin/sites/site
Creating feeds for your blog posts
Feed
feed
RSS
Atom
RSS订阅器
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Creating custom template filters
Django
模板中有很多内建的过滤器{{variable|my_filter}}
{{variable|my_filter:"foo"}}
{{variable|filter1|filter2}}
markdown
语法Python Markdown
模块pip install Markdown==2.6.7
或者用PyCharm
安装blog_tags.py
,增加Django
由于其安全性能,会自动转义<,>,',"",&
,也就是自动转码的功能,在HTML
代码渲染之前,会进行自动转义,然后再呈现出来,如果不进行转义的话,那么XSS
就很容易被利用markdown.markdown()
返回的是字符串格式的HTML
代码,如果不进行mark_safe()
告诉Django
这些代码是安全的话,那么最后面渲染输出的就是转义后的代码,而不是Markdown
格式的内容了post/list.html
和post/detail.html
中导入{% load blog_tags %}
truncatewords_html
过滤器是截取带HTML
标签的内容(不支持中文),自动闭合标签Adding a sitemap to your site
sitemap
文件sitemap
文件相当于给搜索引擎提供网站的目录,便于搜索引擎更有效率的在网站上进行爬行抓取sitemap
文件是一个XML
文件,提供了网站结构,之间的联系Django
有一个sitemap
框架,依赖于django.contrib.sites
,sites
可以将网站和对象进行关联,运行这个框架的话,需要同时激活sitemap
应用 和sites
应用,自定义一个site ID
sites
的表sites
应用已经被同步到数据库,在blog
文件夹下创建sitemaps.py
sitemaps
模块中的Sitemap
类来创建一个自定义的sitemap
changefreq
和priority
这个两个属性表明了post pages
的改变频率和在网站中之间的关联items()
函数返回了所有对象的查询集,默认情况下,Django
利用get_absolute_url()
方法来获得每个post
对象的URL
,如果需要为每个对象指定URL
的话,就要增加一个location()
函数lastmod()
函数通过items()
获取每个对象,返回每个对象最后修改的时间(也就是post
最后的发表时间)sitemap URL
到urls.py
中sitemaps
字典,正则匹配了sitemap.xml
,sitemaps
字典被带入到sitemap
视图中sitemap.xml
中的每个post
URL
都是利用get_absolute_url()
函数获得,lastmod()
函数获取了每个post
最后更新的时间,而域名在这却是example.com
,这个域名来源于Site
对象(同步sites
框架的时候,存储在数据库中)http://localhost:8000/admin/sites/site
,修改为自定义的域名Creating feeds for your blog posts
Feed
是消息源,也就是页面提供一些信息,是一种资料格式,网站透过它将资讯传播给用户,常用的feed
格式有RSS
Atom
两种,用户可以订阅网站的先决条件是,网站提供了消息来源,消息来源又称为源料、馈送、资讯提供、供稿,将feed
汇流于一处成为聚合(RSS
就是一种聚合技术 ),利用聚合器(RSS订阅器
)可以直接查看网站更新内容The text was updated successfully, but these errors were encountered: