Django~Views
2016-02-22 16:44
447 查看
In Django, web pages and other content are delivered by views.
To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns (described as regular expressions) to views.
write views
add urls
404
Each view is responsible for doing one of two things: returning an
PDF,.XML,ZIP ^^^^^^^
Your view can read records from a database, or not. It can use a template system such as Django’s – or a third-party Python template system – or not. It can generate a PDF file, output XML, create a ZIP file on the fly, anything you want, using whatever Python libraries you want.
Create Templates
First, create a directory called
没有加模板前 添加模板后
templates中内容
<ul> for 循环</ul>
省事的render
The
404问题
try except render
加上名字空间
ulsr.py中app_name=’polls’
index 中polls:detail
To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns (described as regular expressions) to views.
django.core.urlresolvers
write views
add urls
404
Each view is responsible for doing one of two things: returning an
HttpResponseobject containing the content for the requested page, or raising an exception such as
Http404.
PDF,.XML,ZIP ^^^^^^^
Your view can read records from a database, or not. It can use a template system such as Django’s – or a third-party Python template system – or not. It can generate a PDF file, output XML, create a ZIP file on the fly, anything you want, using whatever Python libraries you want.
Create Templates
First, create a directory called
templatesin your
pollsdirectory. Django will look for templates in there
名字空间
路径:polls/templates/polls/index.html. .否则
没有加模板前 添加模板后
templates中内容
<ul> for 循环</ul>
{% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="/polls/{{question.id}}/">{{question.question_text}}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> {% endif %}
省事的render
return render(request, 'polls/index.html', context)
The
render()function takes the request object as its first argument, a template name as its second argument and a dictionary as its optional third argument. It returns an
HttpResponseobject of the given template rendered with the given context.
404问题
try except render
加上名字空间
ulsr.py中app_name=’polls’
index 中polls:detail
<li><a href="{% url 'polls:detail' question.id %}">{{question.question_text}}</a></li>
相关文章推荐
- django 文件上传的为空的判定
- 使用Google Guava实现定时缓存功能
- Go如何实现枚举小实例分享
- Emacs golang 配置
- 高德地图叠加google的地图图层
- golang语法总结(十八):处理异常
- golang语法总结(十七):函数
- golang语法总结(十六):map
- golang语法总结(十五):数组array与切片slice
- golang语法总结(十三):选择语句switch
- golang语法总结(十二):循环语句
- golang语法总结(十一):判断语句if
- golang语法总结(十):关于++和--
- golang语法总结(九):运算符、指针
- google play privacy policy
- golang语法总结(八):类型零值、类型别名、类型转换
- golang语法总结(七):常量计数器
- golang语法总结(六):常量声明与赋值
- golang语法总结(五):变量声明与赋值
- golang语法总结(四):基本数据类型