location 最后一个文件夹名就是project名,我用了Django_Plan。

Application 是自动加入的APP名字,我用了Plan

编辑Django_Plan\Django_Plan\urls.py

from django.contrib import admin
from django.urls import path
from Plan import views urlpatterns = [
path('admin/', admin.site.urls),
path('plan', views.plan) #此行为增加的
]

编辑Django_Plan\Plan\views.py

from django.shortcuts import render
from django.shortcuts import HttpResponse #此行增加
# Create your views here.
def plan(request): #此函数增加
return HttpResponse('hello')

好了,我们增加了一个url解析,解析到plan函数,进行http返回给浏览器。

试一下,浏览器http://localhost:8000/plan,会看到hello。用的是HttpResponse函数。

平时访问的页面那么多内容,我们不能都写在HttpResponse中呀。

我们用模板吧。

建立一个hello.html文件放在Django_Plan\templates\hello.html

内容为:

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

编辑Django_Plan\Plan\views.py

from django.shortcuts import render
from django.shortcuts import HttpResponse #此行增加
# Create your views here.
def plan(request): #此函数增加
return render(request,'hello.html')

检查Django_Plan\Django_Plan\settings.py(难道我用最新的django2.0,pycharm就不自动创建了?)

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',
],
},
},
]

好了,这下浏览器返回的就是我们的模板文件内容,h1格式的hello

我们还需要动态生成页面,继续编辑hello.html,给输出的hello加上两个大括号

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

编辑Django_Plan\Plan\views.py

from django.shortcuts import render
from django.shortcuts import HttpResponse #此行增加
# Create your views here.
def plan(request): #此函数增加
return render(request,'hello.html',{'hello':'hello jack'})

这里就是在render的参数中又加了一个字典,把hello换成hello jack,再返回给客户端浏览器。

打开页面试试吧。

Django(三)url和返回的更多相关文章

  1. Django之url路由

    Django之url路由 url路由介绍 url路由是关联url及其视图函数关系的过程. 作用就是使views里面处理数据的函数与请求的url建立映射关系 url路由配置 Django的url路由配置 ...

  2. Django之URL控制器(路由层)

    url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), 一.视图层路由配置系统 URL配置(URLconf)就像Django ...

  3. Django学习笔记之Django的url反向解析

    0x00 URL反向解析和三种不同的反向解析方式 Django中提供了关于URL的映射的解决方案,可以做两个方向的使用: 1.普通解析过程:由客户端的浏览器发起一个url请求,Django根据URL解 ...

  4. Django 02 url路由配置及渲染方式

    Django 02 url路由配置及渲染方式 一.URL #URL #(Uniform Resoure Locator) 统一资源定位符:对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是 ...

  5. django重点url,视图函数,模板语言

    django重点url,视图函数,模板语言url 1.django重点url无命名分组:re_path() 2.url第一个参:url未命别名分组就不需要views中参数一定,若命别名(?P<y ...

  6. django 的auth.authenticate返回为None

    使用auth.authenticate(username= username,passowrd=passowrd),这个用户认证时候,明明数据库中有记录,但是返回就None 我的错误点比较多: 1.我 ...

  7. nodejs笔记三--url处理、Query String;

    URL--该模块包含用以 URL 解析的实用函数. 使用 require('url') 来调用该模块. 一.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看 ...

  8. node.js(三)url处理

    1.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看如下示例: var url = require('url'); url.parse('http://ww ...

  9. Django的url解析

    Django中采用正则表达式来匹配所请求的url,这个叫做URLconf,作为MVC中的C(控制器), 这样再调用相应的试图,达到控制器控制试图的显示的效果. 看一下Django的url解析过程 第一 ...

随机推荐

  1. Python3基础 isinstance 判断是否为指定类的实例对象

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. LA 4254 处理器(二分+贪心)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  3. vijos1904 学姐的幸运数字

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. PHP生成缩略图的一个方法类(转)

    //使用如下类就可以生成图片缩略图 class resizeimage { //图片类型 var $type; //实际宽度 var $width; //实际高度 var $height; //改变后 ...

  5. python filter函数

    number_list = range(-, ) less_than_zero = list(filter(lambda x: x < , number_list)) print(less_th ...

  6. Qt5.3.2(VS2010)_调试_进入Qt源码

    1.必须是 Debug模式 2. http://blog.csdn.net/mayenjoy/article/details/42535789 http://blog.csdn.net/goforwa ...

  7. [ios][swift]UIButton

    参考:http://www.hangge.com/blog/cache/detail_529.html

  8. 探秘AOP实现原理

    可以这么说,AOP是基于动态代理实现的. 那么,这个过程是怎样的? 首先,我们有这样的一个Service类,它是被作为切面的一个类: public class Service implements U ...

  9. 移动端视频h5表现问题汇总

    1. 同屏播放视频 <video src="" x-webkit-airplay="true" webkit-playsinline="true ...

  10. npm 用 淘宝代理

    1.先强制清缓存 npm cache  clean --force 2.运行的npm的指令走的淘宝代理 npm install -g cnpm --registry=https://registry. ...