首页 > 技术文章 > 【Django笔记1】-视图(views)与模板(templates)

tensorzhang 2020-10-23 15:28 原文

视图(views)与模板(templates)

1,视图(views)

​ 将接收到的数据赋值给模板(渲染),再传递给浏览器。HTML代码可以直接放在views.py(文件名可任意更换),也可以放在templates中以实现逻辑代码与HTML文件的解耦。

HTML放在views.py中

  • 创建views.py,之后输入以下代码:

    from django.http import HttpResponse
    from django.shortcuts import render
    
    def HelloWorld(request):
        return HttpResponse('<html><body> hello world! </body></html>')
    
  • 之后在urls.py中配置路由映射关系,输入以下代码:

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url
    from . import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        url(r'^hello/$', views.HelloWorld)
    ]
    
  • 之后打开网址 http://127.0.0.1:8001/hello/ 即可看到如下界面(地址与python manage.py runserver设定的地址相对应)

2,模板(templates)

​ 在1中,HTML文件直接写入了views.py中,也可以将HTML文件写入templates中,实现业务逻辑与前端渲染文件的解耦。

HTML放入templates中

  • 首先在项目中创建templates文件夹,此时项目的目录树结构为

    TEST1
    │  manage.py
    │
    ├─templates
    └─test1
        │  asgi.py
        │  settings.py
        │  urls.py
        │  views.py
        │  wsgi.py
        │  __init__.py
        │
        └─__pycache__
                settings.cpython-36.pyc
                urls.cpython-36.pyc
                views.cpython-36.pyc
                wsgi.cpython-36.pyc
                __init__.cpython-36.pyc
    
    
  • 将templates在settings.py中注册,settings.py进行如下修改:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(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',
                ],
            },
        },
    ]
    
  • 在templates中创建html文件,输入如下代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        hello world! <br>
        {{name}}
    </body>
    </html>
    
  • 修改views.py的代码,此处使用render方法

    from django.http import HttpResponse
    from django.shortcuts import render
    
    def HelloWorld(request):
        return render(request, 'hello.html', {'name': 'tensor_zhang'})	
        #注意此处使用字典填充{{name}}变量,这是django特有的
    
  • 在urls.py配置路由映射,由于我在这没有修改views.py的文件名以及其中的函数名,此次不用修改。

  • 打开网址 http://127.0.0.1:8001/hello/ 即可看到如下界面

3,总结

  1. views和templates将逻辑代码和html文件解耦,这样在对页面进行修改时,只需要专注于修改相应的前端html文件,而不用考虑对逻辑的修改。

  2. 页面的显示是由html文件控制的,只要对html文件进行修改,就可以得到想要的结果,比如将html文件改为:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
        hello world! <br>
        {{name}} <br>
    
        <!-- url跳转 -->
        <a href="https://www.cnblogs.com/tensorzhang/">
            tensor_zhang的博客园
        </a>
    
        <!-- 提交文件 -->
        <form>
            <input type="file" name="myfile">
            <input type="submit" value="提取文件">
        </form>
    
    </body>
    </html>
    

    页面就会相应的发生变化

推荐阅读