代码目录结构
配置文件
__init__.py(djangoSite)
import pymysql
pymysql.install_as_MySQLdb()
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',
'app02',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxx',
'HOST': 'xxx',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '',
}
}
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
逻辑代码
访问顺序
浏览器 --> 接收 url --> urls.py 分析 --> views.py 处理逻辑 --> template 渲染页面/ORM 操作数据库
urls.py
"""
因为是 django3.0.3, 所以 path 和 re_path 是两个不同的方法
"""
from django.contrib import admin
from django.urls import path, re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^index/$', views.index),
re_path(r'^login/$', views.login),
path('publisher_list/', views.publisher_list),
path('publisher_add/', views.publisher_add),
re_path('publisher_edit/', views.publisher_edit),
re_path('publisher_del/', views.publisher_del),
]
views.py(app01)
from django.shortcuts import render, HttpResponse, redirect
from app01 import models
def index(request):
"""
:param request: 所有跟用户请求相关的数据都封装到了一个名为request的对象中
:return:
"""
print(request.method)
print(request.path_info)
return render(request, "index.html")
def login(request):
if request.method == "GET":
return render(request, "login.html")
else:
username = request.POST.get("username", "")
pwd = request.POST.get("password", "")
if username == "111" and pwd == "111":
return redirect("/index/")
else:
return HttpResponse(404)
def publisher_list(request):
data = models.Publisher.objects.all()
print(data)
return render(request, "publisher_list.html", {"data": data, "name": "alex", "b": {"k": "v"}})
def publisher_add(request):
if request.method == "GET":
return render(request, "publisher_add.html")
elif request.method == "POST":
publisher_name = request.POST.get("publisher_name")
models.Publisher.objects.create(name=publisher_name)
return redirect("/publisher_list/")
else:
return HttpResponse("wrong method")
def publisher_edit(request):
if request.method == "GET":
publisher_id = request.GET.get('id')
publisher_data = models.Publisher.objects.get(id=publisher_id)
return render(request, "publisher_edit.html", {"data": publisher_data})
elif request.method == "POST":
publisher_id = request.POST.get("publisher_id")
publisher_name = request.POST.get("publisher_name")
obj = models.Publisher.objects.get(id=publisher_id)
obj.name = publisher_name
obj.save()
return redirect("/publisher_list/")
else:
return HttpResponse("wrong method")
def publisher_del(request):
publisher_id = request.GET.get("id")
models.Publisher.objects.get(id=publisher_id).delete()
return redirect("/publisher_list/")
models.py
from django.db import models
class Publisher(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
def __str__(self):
return self.name
template
publisher_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>出版社列表页面</title>
</head>
<body>
<h1>出版社列表</h1>
<p><a href="/publisher_add/" methods="get">添加新的出版社</a></p>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>出版社 ID</th>
<th>出版社名称</th>
<th>操作</th>
</tr>
</thead>
{# 按照 Django 的特殊语法 写特殊符号, 替换数据#}
<tbody>
{% for obj in data %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ obj.id }}</td>
<td>{{ obj.name }}</td>
<td><a href="/publisher_edit/?id={{ obj.id }}">编辑</a> <a href="/publisher_del/?id={{ obj.id }}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<p>{{ name }}</p>
<p>{{ b.k }}</p>
</body>
</html>
publisher_add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加出版社</title>
</head>
<body>
<form action="/publisher_add/" method="post">
<input type="text" name="publisher_name">
<button type="submit">添加</button>
</form>
</body>
</html>
publisher_edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编辑出版社</title>
</head>
<body>
<form action="/publisher_edit/" method="post">
<input type="text" name="publisher_id" value="{{ data.id }}" style="display: none">
<input type="text" name="publisher_name" value="{{ data.name }}">
<button type="submit">提交</button>
</form>
</body>
</html>
总结
orm
python manage.py makemigrations
python manage.py migrate
model.Publisher.objects.create(name="xxx")
model.Publisher.objects.get(id=1).delete()
obj = models.Publisher.objects.get(id=1)
obj.name = "新值"
obj.save()
model.Publisher.objects.all()
model.Publisher.objerts.get(id=1)
django-html
{
{
render(request, "index.html", {"data":data})
render(request, "index.html", {"data":data, "name": "alex"})
{{ name }}
render(request, "index.html", {"data":data, "name": "alex", "d":{"key": "hahaha"}})
{{ d.key }}
{
{% for obj in data %}
{{ obj.name }}
{{ obj.id }}
{% endfor %}
{
{% if 条件 %}
{% elif 条件 %}
{% else %}
{% endif %}