zoukankan      html  css  js  c++  java
  • Django——优美的Path()函数

    path( )作用:解析URL地址

    path( ) 标准语法: (<>为必须的参数,[]为可选参数)

    path(<route>, <view>, [name=None,**kwargs])   

    下面就具体地址解释:

    https://i.cnblogs.com/EditPosts.aspx?postid=11587136

    route 表示路径,从端口以后的URL地址,到/结束   https://i.cnblogs.com/ 

    从 urlpattern 的第一项开始,按顺序依次匹配列表中的项,直到找到匹配的项。

    view 表示route匹配成功后,需要调用的视图,view 必须是一个函数

    name 表示别名

    **kwargs  表示一个字典,传给view函数

    再次举例:

    文件层次如下图

     其中urls.py

    1 from django.urls import path
    2 from . import views  #将当前目录下的views导入
    3 
    4 urlpatterns = [
    5     #path(route='',view=views.index,name='index'), 第一种完整写法
    6     path('',views.index) #省略后的写法
    7     # 注意views.index不能写出views.index()加了括号就会将index()函数的执行结果返回
    8 ]

    views.py

     1 from django.shortcuts import render
     2 from django.http import HttpResponse
     3 # Create your views here.
     4 
     5 def index(request):
     6     html = '<h1 style="color:red">Hello World! jcx</h1>'
     7     return HttpResponse(html)
     8 
     9 def web(request):
    10     html = '<h1>Django Web </h1>'
    11     return HttpResponse(html)
    ---------------- 生活 > 学习 >> 工作 ----------------
  • 相关阅读:
    Ignatius and the Princess II(全排列)
    中缀式变后缀式
    前缀式计算(前缀表达式)
    Mysql中的锁机制详解
    Mysql关于事务并发带来的问题
    第三方实用API接口汇总
    Mysql 性能优化Explain详解
    Mysql性能分析工具 SHOW PROFILE、 SHOW STATUS
    Mysql慢查询分析
    【数字图像处理】霍夫变换实现
  • 原文地址:https://www.cnblogs.com/jcxioo/p/11587136.html
Copyright © 2011-2022 走看看