django学习02
来源:互联网 发布:公众号分享功能 php 编辑:程序博客网 时间:2024/06/07 11:14
使用html templates实现视图逻辑分离
在相应app目录下建立templates文件夹
1.比如在/virEnv/mysite/polls下建立mkdir -p templates/polls
2.在templates/polls下写html模板文件
3.在views.py文件中,使用loader加载模板,然后使用render将需要展示的数据传递给模板。或者直接使用render将数据与模板路径作为形式参数传递。
e.g:
from django.template import loadertemplate=loader.get_template("polls/index.html")context={ 'latest_question_list':latest_question_list,}return HttpResponse(template.render(context,request))
或者
return render(request,'polls/index.html',context)
4.关于templates中html中例如a标签之类的link路径,/appname/REGname
e.g. /polls/23
或者在urls.py中添加app_name=’appname’然后在 index中直接使用urls.py中的urlname来标记引用的路径 {% url ‘appname:urlname’ REGname %} e.g.{% url ‘polls:detail’ 23 %}
使用css static实现html模板的css设置
在相应app目录下创建static文件夹
1.mkdir -p polls/static/polls
2.vim polls/static/polls/style.css
在templates的index.html中添加对css文件的应用
{% load static %}<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css'%}" />
0 0
- django学习02
- Django学习笔记02
- django学习
- 学习django
- Django 学习
- Django学习
- Django 学习
- django学习
- Django 学习
- django学习
- Django学习
- Django学习
- django学习
- Django学习
- Django学习
- django学习
- django学习
- Django学习
- GPT磁盘及ID号介绍
- [辩论]以成败轮英雄是可取的——正方一辩稿
- 学习python中踩过的那些坑
- Linux常用指令思维导图
- ZTree 增删修改(jsp/servlet)学习系列之一---增加节点
- django学习02
- P2P技术
- minicom键盘失效,不能输入问题
- Hibernate的Annotation注解
- C++中二分查找(递归,非递归)
- 如何设计一个秒杀系统
- linux下安装nvidia driver
- Encoding(二)
- allegro PCB 检查报告中的Report DangLine以及禁用Antenna vias