zoukankan      html  css  js  c++  java
  • django第二个项目--使用模板

    第一步:

      创建新项目secondproject

    cd /tmp/
    django-admin startproject secondeproject

    第二步:

      创建一个用于放置模板文件夹

    cd /tmp/secondeproject/
    mkdir templates
    touch templates/template.html

      template.html内容如下:

    <!doctype html>
    <html>
    <head>
            <title>template page</title>
    </head>
    <body>
            {{ my_var }}
    </body>
    </html>

      修改settings.py 配置模板保存路径

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [BASE_DIR+"/templates/"], #就是这一行,它定义了模板保存的位置。
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    第三步:

      创建view.py文件

    touch /tmp/secondeproject/secondeproject/view.py
    vim /tmp/secondeproject/secondeproject/view.py

      view.py的内容如下

    #!/usr/local/python3.5/bin/python3.5
    
    from django.http import HttpResponse
    from django.shortcuts import render
    
    def fun_print_hello_world(request):
        context={'my_var':'hello world'}
        return render(request,'template.html',context)

    第四步:

      修改urls.py 增加一个hello对应的url

    from django.conf.urls import url
    from django.contrib import admin
    from secondeproject.view import fun_print_hello_world
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^hello/',fun_print_hello_world)
    ]

    第五步:

      访问目标页面

  • 相关阅读:
    总结
    总结
    总结
    总结
    合作成功案例
    本周作业
    上周作业
    本周总结
    本周作业
    分答
  • 原文地址:https://www.cnblogs.com/JiangLe/p/5517055.html
Copyright © 2011-2022 走看看