Django 基本使用
Django 安装
pip install django
Django 创建项目
django-admin startproject 项目名称
Django 创建应用
python manage.py startapp app名称
Django 启动项目
python manage.py runserver IP:PORT
添加一个显示当前时间的功能
- 修改view.py 文件
from django.shortcuts import render,HttpResponse
import datetime
def timer(request):
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return HttpResponse(now)
- 修改urls.py 文件
from django.contrib import admin
from django.urls import path
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
path('timer/', views.timer),
]
跳转到指定HTML页面
- 在templates目录下添加一个HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
登录
</body>
</html>
- 在 view.py 中指定跳转到的页面
from django.shortcuts import render,HttpResponse
def login(request):
return render(request, "login.html")
- 修改 urls.py 文件
from django.contrib import admin
from django.urls import path
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login),
]
获取页面提交的数据
- 在templates目录下添加一个HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8000/auth/" method="post">
{% csrf_token %}
<input type="text" name="name">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
- 在 view.py 中指定跳转到的页面
from django.shortcuts import render,HttpResponse
def login(request):
return render(request, "login.html")
def auth(request):
print(request.POST)
print(request.POST.get("name"))
print(request.POST.get("password"))
return HttpResponse("OK")
- 修改 urls.py 文件
from django.contrib import admin
from django.urls import path
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login),
path('auth/', views.auth),
]
PS:如果没有返回数据可以尝试注释掉 setting.py 中的 'django.middleware.csrf.CsrfViewMiddleware'
获取通过URL传过来的值
- 修改urls.py文件
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r"^login/(w*)$", views.login),
]
- 修改 view.py 文件
from django.shortcuts import render,HttpResponse
def login(request, name):
return HttpResponse(name)
有名分组
- 修改urls.py 文件
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r"^login/(?P<分组名称>w*)$", views.login),
]
- 修改 view.py 文件
from django.shortcuts import render,HttpResponse
def login(request, 分组名称):
return HttpResponse(分组名称)
分发
- 修改urls.py 文件
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'vedio/', include('vedio.urls')),
]
- 在vedio 应用下新建一个urls.py 文件
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^login/', views.login),
]
反向解析
- 修改urls.py 文件
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r"^login/(w*)$", views.login, name='login'),
]
- 修改 login.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8000/{% url 'login' %}" method="post">
<input type="text" name="name">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
Request 和 Response 对象的基本使用
- urls.py 文件的修改
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r"^login/$", views.login, name='login'),
]
- view.py 文件的修改
from django.shortcuts import render,HttpResponse,redirect
def login(request):
"""
request 中的属性和方法
"""
print(request.method) # 请求方式
print(request.path) # 请求路劲
print(request.POST) # POST 请求的数据
print(request.GET) # GET 请求的数据
print(request.META) # 请求头
print(request.get_full_path()) # 请求的完整路径
print(request.is_ajax()) # 判断请求是否是 Ajax请求
"""
response 的三种形式
HttpResponse()
render()
redirect() 重定向
"""
# return HttpResponse('OK')
# return render(request, 'login.html')
# return redirect('login.html')
name = ['GetcharZp', 'GetcharMcx']
# return render(request, 'login.html', locals())
import datetime
now = datetime.datetime.now()
return render(request, 'login.html', {"name": name, "now": now})
- login.html 文件的修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
返回一条数据:
<!-- 深度查询 句点符 -->
{{ name.0 }}
<br>将数据以循环的形式输出:
<ul>
{% for foo in name %}
<li> {{ forloop.late }} {{ forloop.counter }} {{ foo }}</li>
{% endfor %}
<br>当前时间(filter)
<!--
date 时间过滤器 |date:"Y-m-d"
default 默认过滤器 |default:"改内容暂时还木有"
length 长度过滤器 |length
filesizeformat 文件大小过滤器 |filesizeformat
slice 切片过滤器 |slice:"1:3"
truncatechares 字符过滤器 |truncatechares:9
truncatewords 单词过滤器 |truncatewords:7
safe 标识标签不用转义 |safe
-->
{{ now|date:"Y-m-d" }}
</ul>
</body>
</html>
模板继承
- urls.py 文件的修改
from django.contrib import admin
from django.urls import path
from vedio import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]
- view.py 文件的修改
from django.shortcuts import render,HttpResponse,redirect
def index(request):
return render(request, "index.html")
- index.html 文件的修改
{% extends 'base.html' %}
{% block content %}
<p>GetcharZp Index</p>
{% endblock %}
- 新建一个base.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
.header {
100%;
height: 90px;
background-color: #65ccff;
}
</style>
</head>
<body>
<div class="header"></div>
{% block content %}
{% endblock %}
</body>
</html>