1.静态文件资源配置:先到配置文件中把static文件添加到项目的sys.path根目录下
# 接口前缀
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
2.到html文件中导入静态文件
<link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css">
<script src="/static/bootstrap-3.4.1-dist/js/bootstrap.min.js">script>
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
特点:
特点:
1.form表单默认的数据请求方式是get
2.action 数据提交的地址
1.获取请求方式:request.mothod
2.POST
3.GET
DATABASES = {
'default': {'ENGINE': 'django.db.backends.mysql',
'NAME': 'dj02',
'USER': 'root',
'PASSWORD': 'xxx',
'HOST':'127.0.0.1',
'PORT': 3306,
'CHARSET': 'utf8'
}
}
import pymysql
pymysql.install_as_MySQLdb()
类 映射成 表
对象 映射成 记录
对象点属性 映射成 字段对应的值
class User(model.Model): # 类似于定义了表名
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
pwd = models.IntegerField()
注意:主键id也可以不用自己写,ORM会自动帮我面创建一个主键id字段
# 1.查:结果看成是一个列表套数据对象
models.User.objects.filter()
# 如何获取对象 可以使用first()方法 如果没有值会返回None 我们if判断即可
# 2.增
models.User.objects.create()
# 3.改
models.User.objects.filter(id=1).update(name='ming')
# 4.删
models.User.objests.filter(id=3).delete()
detial = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)
publish = models.ForeignKey(to='Publish',on_delete=models.CASCADE)
authors = models.ManyToManyField(to='Ahthors')
1.路由层代码
from django.contrib import admin
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', views.home),
path('insert/', views.insert),
re_path('update/(\d+)/', views.update, name='update'),
re_path(r'^delete/(\d+)/', views.delete, name='delete'),
]
2.视图层代码
from django.shortcuts import render,redirect
# Create your views here.
from app01 import models
def home(request):
data = models.Student.objects.all()
return render(request, 'home.html', locals())
def insert(request):
# 新增数据
if request.method == 'POST':
name = request.POST.get('name')
age = request.POST.get('age')
hobby = request.POST.get('hobby')
models.Student.objects.create(name=name, age=age, hobby=hobby)
return redirect('/home/')
return render(request, 'insert.html')
def update(request, stu_id):
if request.method == 'POST':
name = request.POST.get('name')
age = request.POST.get('age')
hobby = request.POST.get('hobby')
models.Student.objects.filter(pk=stu_id).update(name=name, age=age, hobby=hobby)
return redirect('/home/')
stu_obj = models.Student.objects.filter(pk=stu_id).first()
return render(request, 'update.html', locals())
def delete(request, delete_id):
models.Student.objects.filter(pk=delete_id).delete()
return redirect('/home/')