zoukankan      html  css  js  c++  java
  • Python:Day52 urls

    Django的2.0版本之后使用新的规则,之前的url变成了path,如果要使用正则还需要引入re_path

    from django.urls import path,re_path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path("userInfo",views.userInfo),
        re_path("index(?P<year>[0-9]{4})",views.index,{"user":"alex"},name="happy"),
    ]

    url分发:

    项目下的urls:

    from django.urls import path,re_path,include
    from blog import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('blog/',include("blog.urls")),  //blog后面要加/,不加会报错,注意括号中的格式
    ]

    app下的urls:

    from django.contrib import admin
    from django.urls import path,re_path
    from blog import views
    
    urlpatterns = [
        path('news',views.news),
    ]

    1 HttpRequest对象的属性和方法:

    # path:       请求页面的全路径,不包括域名
    #
    # method:     请求中使用的HTTP方法的字符串表示。全大写表示。例如
    #
    #                    if  req.method=="GET":
    #
    #                              do_something()
    #
    #                    elseif req.method=="POST":
    #
    #                              do_something_else()
    #
    # GET:         包含所有HTTP GET参数的类字典对象
    #
    # POST:       包含所有HTTP POST参数的类字典对象
    #
    #              服务器收到空的POST请求的情况也是可能发生的,也就是说,表单form通过
    #              HTTP POST方法提交请求,但是表单中可能没有数据,因此不能使用
    #              if req.POST来判断是否使用了HTTP POST 方法;应该使用  if req.method=="POST"
    #
    #
    #
    # COOKIES:     包含所有cookies的标准Python字典对象;keys和values都是字符串。
    #
    # FILES:      包含所有上传文件的类字典对象;FILES中的每一个Key都是<input type="file" name="" />标签中                     name属性的值,FILES中的每一个value同时也是一个标准的python字典对象,包含下面三个Keys:
    #
    #             filename:      上传文件名,用字符串表示
    #             content_type:   上传文件的Content Type
    #             content:       上传文件的原始内容
    #
    #
    # user:       是一个django.contrib.auth.models.User对象,代表当前登陆的用户。如果访问用户当前
    #              没有登陆,user将被初始化为django.contrib.auth.models.AnonymousUser的实例。你
    #              可以通过user的is_authenticated()方法来辨别用户是否登陆:
    #              if req.user.is_authenticated();只有激活Django中的AuthenticationMiddleware
    #              时该属性才可用
    #
    # session:    唯一可读写的属性,代表当前会话的字典对象;自己有激活Django中的session支持时该属性才可用。
    
    #方法
    get_full_path(),   比如:http://127.0.0.1:8000/index33/?name=123 ,req.get_full_path()得到的结果就是/index33/?name=123
    req.path:/index33

    注意一个常用方法:request.POST.getlist('')

    2 HttpResponse对象:

      对于HttpRequest对象来说,是由django自动创建的,但是,HttpResponse对象就必须我们自己创建。每个view请求处理方法必须返回一个HttpResponse对象。

      HttpResponse类在django.http.HttpResponse

      在HttpResponse对象上扩展的常用方法:

    1、页面渲染:         render()(推荐)<br>                 render_to_response(),
    2、页面跳转:         redirect("路径")
    3、locals():        可以直接将函数中所有的变量传给模板

    redirect实例(在使用redirect之前要先引入)

    ------------------------------------------urls.py
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('login',views.login),
        path('home',views.home),
    ]
    
    
    ------------------------------------------views.py
    
    def login(request):
        if request.method=="POST":
            if 1:
                return redirect("/home")
        return render(request,"login.html")
    
    
    def home(request):
    
        name="alex"
    
        return render(request,"home.html",{"name":name})
    
    
    ------------------------------------------login.html
    
    <form action="/login" method="post">
        <p>用户名<input type="text" name="username"></p>
        <p>密码<input type="text" name="sex"></p>
        <p><input type="submit" name="submint"></p>
    </form>
    
    
    
    ------------------------------------------home.html
    
    <h1>欢迎{{ name }}回家!</h1>

    locals()参数在render和render_to_response两个里面都能用

        return render(request,"home.html",locals())
        return render_to_response("home.html",locals())
  • 相关阅读:
    codeforces-1328F-Make k Equal
    codeforces-1327C-Game with Chips
    codeforces-1328E-Tree Queries
    深度学习(九):变分自编码器
    深度学习(八):概率生成模型
    深度学习(六):吉布斯采样
    深度学习(五):M-H采样
    深度学习(四):马尔科夫链蒙特卡洛采样(MCMC)
    深度学习(二):图模型的学习
    深度学习(一):概率图模型引入
  • 原文地址:https://www.cnblogs.com/sq5288/p/8989453.html
Copyright © 2011-2022 走看看