简单演示django使用四-创新互联

代码环境接使用三。

创新互联从2013年创立,是专业互联网技术服务公司,拥有项目网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元庄浪做网站,已为上家服务,为庄浪各地企业和个人服务,联系电话:13518219792

先在django中定义模板:

第一步:在项目所在的当前目录创建templates模板目录

  mkdir templates

第二步:在templates目录下,创建与应用同名的目录即应用的模板目录:

  mkdir templates/bookshop

第三步:在应用的模板目录下创建index.html文件

  vim templates/bookshop/index.html

  

  index

  

this is django templates index.html

注意:此html模板文件里可以使用模板语言{% keyworld %}、{{ object }}。

定义模板完成后再使用模板:

第四步:在views.py视图中使用模板

编辑bookshop/views.py文件:

修改为:

#from django.shortcuts import render

from django.http import *

from django.template import RequestContext,loader

def index(request):

   temp = loader.get_template('bookshop/index.html')

   return HttpResponse(temp.render())

  #return HttpResponse('hello world')

或把loader加载过程和宣传过程,由render完成简化了代码,另一种写法为:

from django.shortcuts import render

from django.http import *

def index(request):

   return render(request, 'bookshop/index.html')

第五步:在settings.py设置中,添加模板查找路径

编辑test1/settings.py文件:

修改:

TEMPLATES = [

  {

        ...


    'DIRS': [os.path.join(BASE_DIR,'templates')],

  ...}]

此时,访问浏览器:http://192.168.255.70:8080/

简单演示django使用四

获取数据并传递数据:

在视图views.py中,render中第一个参数是请求,第二个参数是使用的htim模板文件,第三个参数要传递的数据必须使用字典格式:

第一步:向模板中传递数据

编辑bookshoop/views.py文件

修改:

from django.shortcuts import render

from django.http import *

from .models import *

def index(request):

  booklist = BookInfo.objects.all()

   context = {'list':booklist}

   return render(request, 'bookshop/index.html', context)

第二步:在模板中获取数据


编辑templates/bookshop/index.html

修改如下:

  

  index

  {%for book in list %}


  

  • {{book.btitle}}
  •   {%endfor%}

      

    this is django templates index.html

    第三步:刷新浏览器

    访问浏览器:http://192.168.255.70:8080/

    简单演示django使用四

    使用更多模板语言:

    第一步:编辑templates/bootshop/index.html模板文件

      

      index

      

      

    this is django templates index.html

    第二步:修改url路由配置

    编辑bookshop/urls.py文件:

    from django.conf.urls import url

    from . import views

    urlpatterns = [

      url(r'^$',views.index),

      url(r'^(\d+)$',views.show),#正则匹配到的结果是当做一个show的参数

    ]

    第三步:添加视图函数

    编辑bookshop/views.py文件:

    添加:

    def show(request,id):#该函数一共要2个参数,其中id为正则式传递的参数

      book = BookInfo.objects.get(pk=id)

      herolist = book.heroinfo_set.all()

      context = {'list':herolist`}

      return render(request,'bookshop/show.html',context)

    第四步:添加show.html模板文件


    编辑templates/bookshop/show.html文件:

      

      Title

      

          {%for hero in list%}

          

    • {{hero.hname}}
    •     {%endfor%}

        

    第五步:测试

    浏览器访问:http://192.168.255.70:8080/

    简单演示django使用四

    为测试添加数据:

    浏览器访问:http://192.168.255.70:8080/admin/

    添加Hero infos信息,点击Hero infos:

    简单演示django使用四

    然后,点击:增加hero info

    简单演示django使用四

    填写hero info相关信息即可:

    简单演示django使用四

    填写完成后,保存即可。

    浏览器访问测试:http://192.168.255.70:8080/

    简单演示django使用四

    单击链接标签,如python,注意浏览器地址的变化为:http://192.168.255.70:8080/2

    简单演示django使用四

    测试成功。

    另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


    网站题目:简单演示django使用四-创新互联
    URL链接:http://lszwz.com/article/cddhgh.html

    其他资讯

    售后响应及时

    7×24小时客服热线

    数据备份

    更安全、更高效、更稳定

    价格公道精准

    项目经理精准报价不弄虚作假

    合作无风险

    重合同讲信誉,无效全额退款