标签:nbsp pattern context 字典 install image manage site stp
1.安装django pip install django==1.8.2
2.检验是否安装成功 import django 然后print(django.VERSION)
3.创建项目: django-admin startproject 项目名(项目名时不能以数字开头的)
4.创建app: python manage.py startapp app01
6.在app下创建static用来存放静态文件(CSS.js.images) template用来存放html
5.配置django的相关配置
a.settings: tempaltes中加入 [os.path.join(base_dir,"template").replace("\\","/")] 用来加入template的路径
b.views中:
第一种方法
def get_page(request):
return render(request,‘newslistpic.html‘,locals())
def get_include(request):
return render(request,‘get_include.html‘,locals())
‘‘‘
class Teacher(object):
def student(self):
return "dasdadasdsdsd"
第二种方法
def get_page(request):
# 使用get_template方法加载templa下的html
template = get_template("newslistpic.html")
# 构造后端需要传递的参数的值
html_date = {
"name":"litao",
"age":18,
"project":["python","javascrapy","java","php"],
"company":{"name":"aili","position":"hangzhou"},
"methon":Teacher,
}
# 定义后端传递的参数,用Context格式化需要传递到前端的参数,注意是以字典的形式传递
contect = Context({})
result = template.render(contect)
# 模板加载数据
return HttpResponse(result)
‘‘‘
在urls中:
from django.conf.urls import include, url
from django.contrib import admin
from first_prject.views import get_page,get_include
urlpatterns = [
url(r‘^admin/‘, include(admin.site.urls)),
url(r‘^get_page/‘,get_page),
url(r‘get_include/‘,get_include),
]
标签:nbsp pattern context 字典 install image manage site stp
原文地址:https://www.cnblogs.com/litao201403/p/10128334.html