1:一个简单的web框架
# 导包 from wsgiref.simple_server import make_server #自定义个处理函数 def application(environ,start_response): start_response("200 OK",[(‘Content-Type‘,‘text/html‘)]) return [b‘<h1>Hello,web!</h1>‘] httpd = make_server(‘‘,8091,application) print(‘Serving HTTP on port 8091....‘) httpd.serve_forever()
HelloWorld
# 导包 from wsgiref.simple_server import make_server #自定义个处理函数 def application(environ,start_response): # 获取路径 path = environ["PATH_INFO"] start_response("200 OK",[(‘Content-Type‘,‘text/html‘)]) if path=="/yang": return [b‘<h1>Hello,yang!</h1>‘] elif path=="/Aaron": return [b‘<h1>Hello,aaron!</h1>‘] else: return [b‘<h1>404!</h1>‘] httpd = make_server(‘‘,8091,application) print(‘Serving HTTP on port 8091....‘) httpd.serve_forever()
2.0
# 导包 from wsgiref.simple_server import make_server def yang(): f=open("yang.html","rb") data=f.read() return data def aaron(): f=open("aaron.html","rb") data=f.read() return data #自定义个处理函数 def application(environ,start_response): # 获取路径 path = environ["PATH_INFO"] start_response("200 OK",[(‘Content-Type‘,‘text/html‘)]) if path=="/yang": return [yang()] elif path=="/Aaron": return [aaron()] else: return [b‘<h1>404!</h1>‘] httpd = make_server(‘‘,8091,application) print(‘Serving HTTP on port 8091....‘) httpd.serve_forever()
调用HTML内容
# 导包 import time from wsgiref.simple_server import make_server def region(req): pass; def login(req): print(req["QUERY_STRING"]) f=open("login.html",‘rb‘) data=f.read(); return data; def yang(req): f=open("yang.html","rb") data=f.read() return data def aaron(req): f=open("aaron.html","rb") data=f.read() return data def show_time(req): times=time.ctime() # 方法一:通过模板使用 # con=("<h1>time:%s</h1>" %str(times)).encode("utf8") # return con # 方法二:字符串替换 f = open("show_time.html", "rb") data = f.read() data=data.decode("utf8") data =data.replace("{{time}}",str(times)) return data.encode("utf8") # 定义路由 def router(): url_patterns=[ ("/login",login), ("/region", region), ("/yang", yang), ("/aaron", aaron), ("/show_time",show_time), ] return url_patterns #自定义个处理函数 def application(environ,start_response): # 获取路径 path = environ["PATH_INFO"] start_response("200 OK",[(‘Content-Type‘,‘text/html‘)]) url_patterns = router() func =None for item in url_patterns: if item[0]==path: func=item[1] break if func: return [func(environ)] else: return [b‘404‘] httpd = make_server(‘‘,8091,application) print(‘Serving HTTP on port 8091....‘) httpd.serve_forever()
模拟路由
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } </style> </head> <body> <h1>时间:{{time}}}</h1> </body> </html>
show_time.html
2:一个简单的django案例
Django的下载与安装
如何检验是否安装成功?
2.1 创建django项目的两种方法
--创建Django项目 django-admin startproject mysite --创建应用 python manage.py startapp blog
通过命令创建
方式2:通过Pycharm创建
创建成功
大致分为三步
a:修改urls.py 类似控制器,把想要展示的内容通过地址配置一下
b:在views中设置具体的逻辑
c:在templates中设置要显示的页面内容
通过命令行启动django。
python manage.py runserver 8091
如何引用js
a:添加static文件,并把js放置到该文件下
b:在setting文件中配置
c:在对应的文件中做引用
URL配置(URLconf):又叫做路由系统,其本质是提供路径和视图函数之间的调用映射表。
格式:
urlpatterns=[
url(正在表达式,views视图函数,参数,别名)
]
例1:匹配 XXX/articles/年份(只能匹配4位数字)
from django.contrib import admin from django.urls import path from django.conf.urls import url from blog import views urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘show_time/‘,views.show_time), url(r‘^articles/[0-9]{4}/$‘, views.year_archive), ]
urls.py--1.0
from django.shortcuts import render,HttpResponse import time def show_time(request): # return HttpResponse("Hello") return render(request,"index.html",{"time":time.ctime()}) # Create your views here. def year_archive(request): return HttpResponse("2018");
Views.py
例2:如何获取到地址栏中的年份(通过路由添加()匹配)
例3:给分组命名
urls中的配置
url(r‘^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})$‘, views.year_archive),
views视图中的代码
return HttpResponse(year+"-"+month)
例四:注册小练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> <p>姓名 <input type="text" name="name"></p> <p>年龄 <input type="text" name="age"></p> <p>爱好 <input type="checkbox" name="hobby" value="1">读书 <input type="checkbox" name="hobby" value="2">写字 <input type="checkbox" name="hobby" value="3">看报 </p> <p><input type="submit"></p> </form> </body> </html>
Register.html
from django.shortcuts import render,HttpResponse import time def show_time(request): # return HttpResponse("Hello") return render(request,"index.html",{"time":time.ctime()}) # Create your views here. def year_archive(request,month,year): return HttpResponse(year+"-"+month) def Register(request): if request.method=="POST": con="Hello,%s,你的年龄是%s"%(request.POST.get("name"),request.POST.get("age")) return HttpResponse(con) return render(request,"Register.html")
Views.py
"""django01 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/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.contrib import admin from django.urls import path from django.conf.urls import url from blog import views urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘show_time/‘,views.show_time), url(r‘^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})$‘, views.year_archive), url(r‘^Register/‘, views.Register), ]
urls.py
注意:需要把这句代码给注释掉
效果图
在url中给地址设置一个别名,这样后期Register名称的修改将不影响系统中其他调用的功能
URL分发
效果:
原文地址:https://www.cnblogs.com/YK2012/p/10074749.html
时间: 2024-10-09 02:21:51