zoukankan      html  css  js  c++  java
  • django创建第一个视图-4

    创建视图

    打开 demo 应用下的 views.py 文件,添加代码

    from django.http import HttpResponse
    from django.shortcuts import render
    
    # Create your views here.
    
    
    def index(request):
        return HttpResponse("hello world")
    
    • 视图函数的第一个参数必须定义,用于接受django请求数据的 HttpRequest 对象,通常命名为 request

    • 视图函数必须返回一个相应对象,如果要返回字符串,可以把字符串放到 HttpRequest 对象中

    创建路由

    在子应用目录下新建 urls.py 文件,添加代码

    from django.conf.urls import url
    from . import views
    
    urlpatterns = [
        url(r'^', views.index, name="index")
    ]
    

    注册路由

    在项目目录下(django_project)的 urls.py 中添加代码

    from django.conf.urls import url, include
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^demo/', include("demo.urls"))
    ]
    

    在浏览器中访问 http://127.0.0.1:8000/demo/,就能看到我们返回的信息 hello world

  • 相关阅读:
    Java第一课
    bootstrap之google fonts
    bootstrap之clearfix
    Leetcode题解
    python图片爬虫
    [python / selenium]
    使用python
    python
    python
    python爬虫入门新手向实战
  • 原文地址:https://www.cnblogs.com/mxuanli/p/9814565.html
Copyright © 2011-2022 走看看