Python学习第二十八课——Django(urls)
Django框架中的urls配置:
首先通过pycharm创建一个Django项目:

例如要写blog的功能:则在digango_lesson中的urls代码如下:
"""django_lesson URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from blog import views urlpatterns = [
path('admin/', admin.site.urls),
path('show_time/', views.show_time), path('blog/', include('blog.urls')), # 将urls 进行分发 发到blog文件夹下的urls ]
blog项目功能的urls 全部写在 blog文件夹下的urls,如下:
"""django_lesson URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from blog import views urlpatterns = [
url(r'article/(\d{4})$/(\d{2})', views.article_year),
url(r'article/(?P<year>\d{4})$/(?P<mouth>\d{2})', views.article_year_mouth), # 这种写法参数必须起尖括号里面的名字
url(r'article/(?P<year>\d{4})/(?P<mouth>\d{2})/(?P<day>\d{2})', views.article_year_mouth_day), # 这种写法参数必须起尖括号里面的名字
url(r'register', views.register,name="reg"),
# url(r'login', views.login,name="log"), ]
写完url后 要在views中完成功能代码:
from django.shortcuts import render, HttpResponse import time # Create your views here.
def show_time(request):
t = time.ctime()
# return HttpResponse("Hellow")
return render(request, "index.html", {"time": t}) def article_year(request, y, m):
return HttpResponse("日期:%s年 %s月" % (y, m)) def article_year_mouth(request, year, mouth):
return HttpResponse("这个日期日期:%s年 %s月" % (year, mouth)) def article_year_mouth_day(request, year, mouth, day):
return HttpResponse("现在的日期:%s年 %s月 %s日" % (year, mouth, day)) def register(request):
if request.method == "POST":
print(request.POST.get("user")) # user
print(request.POST.get("pwd")) #
return HttpResponse("success!") return render(request, "register.html") # def login(request):
# username = request.GET.get("user")
# password = request.GET.get("pwd")
# if username == "hanhan" and password == "123":
# return render(request, "successful.html")
# else:
# return render(request, "register.html")
用到html应该全部放在templates文件夹中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>学生注册</h1>
<form action="{% url 'reg' %}" method="post">
<!--{% url 'reg' %} 对应的是blog下面的urls url(r'register', views.register,name="reg"),-->
<p>用户名<input type="text" name="user"></p>
<p>密码 <input type="text" name="pwd"></p>
<p>爱好 <input type="checkbox" name="hobby">篮球
<input type="checkbox" name="hobby">足球
<input type="checkbox" name="hobby">乒乓球
</p> <p><input type="submit"></p> </form> </body>
</html>
如果要加入JS,等文件则需要在settings中配置:
# 加在末尾即可
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "blog/static"),
) # 逗号很重要 static相当于包的名字 固定这样写 才可以找到
HTML中用JS代码如下:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> {# {% load staticfiles %}#} <title>Title</title>
</head>
<body> <h1>Django,你好,现在时间为:{{time}}</h1>
{#第一种 执行Script 文件 #}
<script src="/static/jquery-3.1.1.js"></script> <!--要放到处理代码后面 --> {#第二种#}
{#<script src="{% static 'jquery-3.1.1.js' %}"></script>#} <script>
$("h1").css("color","red") </script> </body>
</html>
Python学习第二十八课——Django(urls)的更多相关文章
- Python学习第二十八课——Django(templates)
templates 讲后台得到的数据渲染到页面上:话不多说,先看具体代码. urls: from django.conf.urls import url from django.contrib imp ...
- Python学习第二十六课——PyMySql(python 链接数据库)
Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...
- Python学习第二十五课——Mysql (多表查询)
多表查询: 内连接查询: 首先:创建两个表一个为tableA,一个为tableB,并且插入数据(代码省略) 同时查询两个表的记录: select * from tableA,tableB; 根据tab ...
- Python学习第二十四课——Mysql 外键约束
外键:主要是关联两个表的 举个栗子:在建表中创建外键 -- 添加外键例子 CREATE TABLE teacher( id TINYINT PRIMARY KEY auto_increment, na ...
- Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)
记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...
- Python学习第十八课——继承,接口继承等
1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...
- NeHe OpenGL教程 第二十八课:贝塞尔曲面
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 风炫安全web安全学习第二十八节课 CSRF攻击原理
风炫安全web安全学习第二十八节课 CSRF攻击原理 CSRF 简介 跨站请求伪造 (Cross-Site Request Forgery, CSRF),也被称为 One Click Attack 或 ...
- 风炫安全WEB安全学习第二十三节课 利用XSS获取COOKIE
风炫安全WEB安全学习第二十三节课 利用XSS获取COOKIE XSS如何利用 获取COOKIE 我们使用pikachu写的pkxss后台 使用方法: <img src="http:/ ...
随机推荐
- webpack: webpack简单打包后的代码(1)
源码 本文研究的源码地址为:https://github.com/collect-webpack/practice/tree/master/webpack-01 在本研究的前提是 entry 的配置为 ...
- dw 快捷键
<html></html> 创建一个HTML文档<head></head> 设置文档标题和其它在网页中不显示的信息<title></t ...
- HDU 1326 Box of Bricks(思维)
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stac ...
- 在spring Boot中使用swagger-bootstrap-ui(原文)
1.swagger简介 Swagger是一个API接口管理工具,支持在线测试接口数据,根据配置自动生成API文档,结合spring mvc而提供界面化方法文档的一个开源框架. 1.1Swagger主要 ...
- STL总结 (C++)
一.一般介绍 STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库.它被容纳于C++标准程序库(C++ Standard Library ...
- jmeter实现IP欺骗
用jmeter模拟多个IP同时向一个目标发送请求 1.IP地址参数化 在csv文件中编辑参数化IP地址列表,参数化的IP需在同一个局域网,子网掩码相同(比如和客户端本机同一网段),如下 将csv列表中 ...
- pwnable.kr-flag-Writeup
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- AppBoxFuture: Sql存储的ORM查询示例
上篇介绍集成第三方Sql数据库时未实现如导航属性.子查询等功能,经过大半个月的努力作者初步实现了这些功能,基本上能满足80%-90%查询需求,特别复杂的查询可以用原生sql来处理,下面分别示例介绍 ...
- mybatis - buildSqlSessionFactory()
buildSqlSessionFactory() 这个方法比较长, 干的事情也比较多. 包括一些别名, 插件, 类型处理器等的解析. 从主流程上来看, 最主要的其实是干了两件事:1. 对 mapper ...
- Apache的虚拟主机功能(基于IP地址、基于虚拟主机、基于端口)
1. 安装Apache服务程序(系统用户,1-199之间) 第一步:在虚拟机软件里选中光盘镜像: 第二步:将光盘设备挂载到/media/cdrom目录 输入:mkdir -p /media/cdrom ...