美文网首页
python web页面跳转

python web页面跳转

作者: 初了谁的眼 | 来源:发表于2019-05-27 18:07 被阅读0次

    接到项目需求。需要搭建一个页面进行交互,慢慢来


    b (2).jpg

    使用python django框架进行页面的搭建
    在项目文件下打开窗口,输入命令;

      django-admin startproject helloword
      #在文件helloword/helloword/创建view.py
    

    在view.py文件中输入以代码

    from django.shortcuts import render
    def hello(request):
        context = {}
        context['hello'] = '终端集成控制系统'
        return render(request, 'index.html', context)
    

    在settings文件中进行TEMPLATES 项目的配置:

    'DIRS': [BASE_DIR+"/templates",], #修改位置
    

    在urls中创建如下配置:

    from django.urls import path
    from . import view
    
    urlpatterns = [
        path('index/', view.hello),
    

    因为已经配置完成了dirs,所以需要在helloword文件夹下。创建templates文件
    在此文件夹下,创建index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>{{ hello }}</title>
    </head>
    <body>
            <h1>{{ hello}}</h1>
    </body>
    </html>
    

    简单的页面跳转,制作完成。最主要的是路由的配置,就是settings里面需要设置的dirs.还有urls里面的路由。

    相关文章

      网友评论

          本文标题:python web页面跳转

          本文链接:https://www.haomeiwen.com/subject/eacktctx.html