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)的更多相关文章

  1. Python学习第二十八课——Django(templates)

    templates 讲后台得到的数据渲染到页面上:话不多说,先看具体代码. urls: from django.conf.urls import url from django.contrib imp ...

  2. Python学习第二十六课——PyMySql(python 链接数据库)

    Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...

  3. Python学习第二十五课——Mysql (多表查询)

    多表查询: 内连接查询: 首先:创建两个表一个为tableA,一个为tableB,并且插入数据(代码省略) 同时查询两个表的记录: select * from tableA,tableB; 根据tab ...

  4. Python学习第二十四课——Mysql 外键约束

    外键:主要是关联两个表的 举个栗子:在建表中创建外键 -- 添加外键例子 CREATE TABLE teacher( id TINYINT PRIMARY KEY auto_increment, na ...

  5. Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)

    记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...

  6. Python学习第十八课——继承,接口继承等

    1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...

  7. NeHe OpenGL教程 第二十八课:贝塞尔曲面

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  8. 风炫安全web安全学习第二十八节课 CSRF攻击原理

    风炫安全web安全学习第二十八节课 CSRF攻击原理 CSRF 简介 跨站请求伪造 (Cross-Site Request Forgery, CSRF),也被称为 One Click Attack 或 ...

  9. 风炫安全WEB安全学习第二十三节课 利用XSS获取COOKIE

    风炫安全WEB安全学习第二十三节课 利用XSS获取COOKIE XSS如何利用 获取COOKIE 我们使用pikachu写的pkxss后台 使用方法: <img src="http:/ ...

随机推荐

  1. ffmpeg-- audio decoder

    测试代码来源于:http://ffmpeg.org/doxygen/trunk/decode_audio_8c-example.html /* * Copyright (c) 2001 Fabrice ...

  2. Lenet 神经网络-实现篇(2)

    Lenet 神经网络在 Mnist 数据集上的实现,主要分为三个部分:前向传播过程(mnist_lenet5_forward.py).反向传播过程(mnist_lenet5_backword.py). ...

  3. kali 安装与配置

    打开虚拟机 新建一个虚拟机 导入虚拟文件 然后进行下面的步骤 开启虚拟机 语言:中文简体 地区: 中国 语言: 汉语 自动安装 配置网络 配置域名 填写密码(两次一致) 自动校对时钟 使用整个磁盘 选 ...

  4. 矩阵快速幂+概率DP poj 3744

    题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

  5. HDU 2586(LCA欧拉序和st表)

    什么是欧拉序,可以去这个大佬的博客(https://www.cnblogs.com/stxy-ferryman/p/7741970.html)巨详细 因为欧拉序中的两点之间,就是两点遍历的过程,所以只 ...

  6. Mac 配置cron

    请参考:https://www.cnblogs.com/EasonJim/p/7819635.html 查看 crontab 是否启动 sudo launchctl list | grep cron ...

  7. Go文件拷贝

    package main import ( "os" "io" "fmt" "io/ioutil" ) func mai ...

  8. 汪莹:以RELX悦刻为例,复盘中国品牌出海的跨文化挑战

    海外销售额每月2倍增速,3个月拿下东南亚市场第一,出口43个国家,拥有250万用户--你可能不知道,这是一家成立仅一年半.出海仅7个月的中国企业交出的答卷. 这家企业就是中国第一大电子烟品牌RELX悦 ...

  9. maven版cxf集合spring开发服务端(二)

    一.新建一个maven项目 二.pom.xml引入依赖 <dependency> <groupId>org.apache.cxf</groupId> <art ...

  10. mybatis - 执行 getById

    1. getById 的执行 前面一篇 提到过, Mapper.java 创建的时候, 会通过 jdk 代理的方式来创建, 且代理处理类为: MapperProxy . 所以当执行 UserMappe ...