Django(三)url和返回
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和返回的更多相关文章
- Django之url路由
Django之url路由 url路由介绍 url路由是关联url及其视图函数关系的过程. 作用就是使views里面处理数据的函数与请求的url建立映射关系 url路由配置 Django的url路由配置 ...
- Django之URL控制器(路由层)
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), 一.视图层路由配置系统 URL配置(URLconf)就像Django ...
- Django学习笔记之Django的url反向解析
0x00 URL反向解析和三种不同的反向解析方式 Django中提供了关于URL的映射的解决方案,可以做两个方向的使用: 1.普通解析过程:由客户端的浏览器发起一个url请求,Django根据URL解 ...
- Django 02 url路由配置及渲染方式
Django 02 url路由配置及渲染方式 一.URL #URL #(Uniform Resoure Locator) 统一资源定位符:对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是 ...
- django重点url,视图函数,模板语言
django重点url,视图函数,模板语言url 1.django重点url无命名分组:re_path() 2.url第一个参:url未命别名分组就不需要views中参数一定,若命别名(?P<y ...
- django 的auth.authenticate返回为None
使用auth.authenticate(username= username,passowrd=passowrd),这个用户认证时候,明明数据库中有记录,但是返回就None 我的错误点比较多: 1.我 ...
- nodejs笔记三--url处理、Query String;
URL--该模块包含用以 URL 解析的实用函数. 使用 require('url') 来调用该模块. 一.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看 ...
- node.js(三)url处理
1.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看如下示例: var url = require('url'); url.parse('http://ww ...
- Django的url解析
Django中采用正则表达式来匹配所请求的url,这个叫做URLconf,作为MVC中的C(控制器), 这样再调用相应的试图,达到控制器控制试图的显示的效果. 看一下Django的url解析过程 第一 ...
随机推荐
- Python3基础 str count 获得子字符串出现的次数
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 打开vi后提示The ycmd server SHUT DOWN (restart with :YcmRestartServer)该如何处理
答:进入YouCompleteMe的安装目录安装一些必要的依赖 比如:笔者将YouCompleteMe安装到了~/.vim/bundle目录下,那么执行以下操作: cd ~/.vim/bundle/Y ...
- JS事件监听器
JS事件监听器 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Javasc ...
- github issues的操作
https://help.github.com/en/articles/searching-issues-and-pull-requests 根据 reporter筛选issues https://h ...
- 51nod 1201 整数划分 基础DP
1201 整数划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} ...
- Python Sip [RuntimeError: the sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3]
不知道原因,尝试卸载.编译安装均失败.只有这样曲线救国 import matplotlib matplotlib.use("WXAgg",warn=True) import mat ...
- 同一主机配置:vsftpd+pam+mysql
两种情况: 1.vsftpd和MySQL不在同一台主机上 vsftpd服务器和MySQL服务器不在同一台主机上时,vsftpd服务器需要安装pam_mysql. 在指定用户认证时,vsftpd需要链接 ...
- 01_Flume基本架构及原理
Flume消息收集系统,在整个系统架构中的位置 Flume概况1) Apache软件基金会的顶级项目2)存在两个大的版本:Flume 0.9.x(Flume-OG,original generatio ...
- js事件轮询机制
console.log(1) setTimeout(function(){ console.log(2) },0); console.log(3) 毫无疑问:运行结果是1 3 2 也就是说:setTi ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...