1.母版的继承
#base { % block title %}Title { % endblock %} #index,html #必须是 extends 这个关键字 ,后边跟着继承的哪个页面 #{%extends "base.html"%}
{% extends "base.html" %} {% block content %} { { block.super }} {% endblock %} {% block title %}首页 {% endblock %}
2.orm小的项目整理
2.1新建的项目里先生成表
#modelsclass Book(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=32) price=models.DecimalField(max_digits=8,decimal_places=2) # 999999.99 pub_date=models.DateTimeField() # "2012-12-12" publish=models.ForeignKey(to="Publish",on_delete=models.CASCADE) # 级联删除 authors=models.ManyToManyField(to="Author") def __str__(self): return self.titleclass Publish(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField(max_length=32) email=models.CharField(max_length=32) def __str__(self): return self.nameclass Author(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField(max_length=32) age=models.IntegerField() email=models.CharField(max_length=32) ad=models.OneToOneField(to="AuthorDetail",on_delete=models.CASCADE) def __str__(self): return self.nameclass AuthorDetail(models.Model): addr=models.CharField(max_length=32) tel=models.IntegerField() #author=models.OneToOneField("Author",on_delete=models.CASCADE) def __str__(self): return self.addr
2.2进行数据库的迁移
2.3添加表里的数据,先从从表里添加数据,再从主表里添加数据,或者直接粗暴的在数据库里添加
2.4分配路由,写视图函数
#urlsfrom django.contrib import adminfrom django.urls import path,re_pathfrom app01 import viewsurlpatterns = [ path('admin/', admin.site.urls), path("books/",views.book_view), re_path("^$",views.book_view), path("books/add/",views.book_add), re_path("^books/edit/(?P\d+)$",views.book_edit), re_path("^books/delete/(?P \d+)$",views.book_del),]
2.5函数
def book_view(request): book_list=Book.objects.all() return render(request,"book_view.html",{ "book_list":book_list})def book_add(request): if request.method=="GET": publish_list=Publish.objects.all() author_list=Author.objects.all() return render(request,"book_add.html",{ "publish_list":publish_list,"author_list":author_list}) else: title=request.POST.get("title") price=request.POST.get("price") pub_date=request.POST.get("pub_date") publish_id=request.POST.get("publish_id") authors=request.POST.getlist("authors") print(request.POST) print(authors) book=Book.objects.create(title=title,price=price,pub_date=pub_date,publish_id=publish_id) book.authors.add(*authors) return redirect("/books/")def book_edit(request,edit_book_id): edit_book = Book.objects.filter(pk=edit_book_id).first() if request.method=="GET": publish_list = Publish.objects.all() author_list = Author.objects.all() return render(request,"book_edit.html",{ "edit_book":edit_book,"publish_list":publish_list,"author_list":author_list}) else: title = request.POST.get("title") price = request.POST.get("price") pub_date = request.POST.get("pub_date") publish_id = request.POST.get("publish_id") authors = request.POST.getlist("authors") print(request.POST) print(authors) Book.objects.filter(pk=edit_book_id).update(title=title,price=price,pub_date=pub_date,publish_id=publish_id) edit_book.authors.set(authors) return redirect("/books/")def book_del(request,del_book_id): Book.objects.filter(pk=del_book_id).delete() return redirect("/books/")
2.6 tmplates
#book_add.htmlTitle 添加书籍
#book_edit.htmlTitle 编辑书籍
#book_view.htmlTitle 查看书籍
添加书籍
{ % for book in book_list %} 编号 书籍名称 价格 出版日期 出版社 作者 操作 { % endfor %} { { forloop.counter }} { { book.title }} { { book.price }} { { book.pub_date|date:"Y-m-d" }} { { book.publish.name }} { % for author in book.authors.all %} { { author.name }} { % if not forloop.last %} , { % endif %} { % endfor %} 删除 编辑
3json模块
3.1json其实就是一个模块,把字符串类型的转换成我们想要的数据类型
d = { "name":"alex"}print(type(d))#s = json.dumps(d)print(type(s))# #注意:json中的dumps是把其他数据类型转换成字符串数据类型#注意:json中的loads是把字符串数据类型转换成原有的数据类型
4.AJAX
4.1利用Ajax请求进行简单的求和(没有动态传参的Ajax)
#urls path('index/', views.index),#views中def index(request): return render(request,"index.html")#templayes中Title INDEX页面
{ % csrf_token %} + =
// 传参Ajax请求 $(".cal").click(function () { var num1=$("#num1").val(); var num2=$("#num2").val(); $.ajax({ #返回的地址 url:"/cal/", type:"post", data:{ num1:num1, num2:num2, csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val() }, success:function (response) { console.log(response); $("#ret").val(response) } }) })
#urls
path('cal/', views.cal), #views中
def cal(request): num1=request.POST.get("num1") num2=request.POST.get("num2") ret=int(num1)+int(num2) return HttpResponse(str(ret))
4.2利用Ajax进行登录
4.2.1分配路由写视图函数
def login(request): if request.method == "POST": res={ "user":None,"error":""} user=request.POST.get("user") pwd=request.POST.get("pwd") user_obj=UserInfo.objects.filter(user=user,pwd=pwd).first() if user_obj: res["user"]=user else: res["error"]="用户名或者密码错误!" return HttpResponse(json.dumps(res)) else: return render(reqeust,"login.html") #templades中
Title