django系列笔记
说明:本文环境windows+pycharm专业版
1.HelloDjando
环境搭建:为django项目创建自己独立的python虚拟环境,并在pycharm中选择解释器为刚刚创建的python虚拟环境
进入到要创建django项目的目录,执行如下命令
django-admin startproject HelloDjango #创建项目
python manage.py startapp App #创建应用,注意创建后先把该应用注册到settings.py中,以免后续忘记
python manage.py runserver #启动服务
浏览器访问:http://127.0.0.1:8000/ 可看到django的欢迎界面
2.MTV
模板配置:需要在项目目录中创建templates文件夹并标记;需要在settings中进行注册
路由优化配置:拆分为多个app;继承拆分路由器
例子1:
浏览器访问:http://127.0.0.1:8000/hello/
例子2:
项目根目录下创建文件夹templates(如果想让代码自动提示,需要右击templates Mark Directory as Templates)
访问: http://127.0.0.1:8000/home/ 可看到如下界面
3.model的介绍与简单使用
Object Relational Mapping 对象关系映射
迁移的概念:就是将模型映射到数据库的过程
编写models.py后,执行如下命令:
python manage.py makemigrations #生成迁移
python manage.py migrate #执行迁移
如何使用mysql数据库?(django默认的是sqlite3)
1.修改setting.py文件 2.安装pymysql ,pip install pymysql(如果原来已安装的话不需要安装) 3.在 项目根目录/HelloDjango/__init__.py增加如下内容: import pymysql pymysql.install_as_MySQLdb()
1.首先编写应用下的models.py from django.db import models # Create your models here. class Student(models.Model): s_name = models.CharField(max_length=16) s_age = models.IntegerField(default=1) 2.应用下新建urls.py并写入如下代码(注意鼠标定位到函数上就可以使用快捷键alt+enter进行快速导包与创建view对应的函数) from django.conf.urls import url from Two import views urlpatterns = [ url(r'^index/',views.index), url(r'^addstudent/',views.add_student), url(r'^getstudents/',views.get_students), url(r'^updatestudent/',views.update_student), url(r'^deletestudent/',views.delete_student), ] 3.项目下的urls.py增加如下 url(r'^two/',include('Two.urls')), 4.编写应用下的views.py import random from django.http import HttpResponse from django.shortcuts import render # Create your views here. from Two.models import Student def index(request): return HttpResponse("Two index") def add_student(request): student = Student() student.s_name = 'Tom%d'%random.randrange(100) student.save() return HttpResponse("Add Success %s"%student.s_name) def get_students(request): students = Student.objects.all() for student in students: print(student.s_name) context = { "hobby":"play", "students":students } return render(request,"student_list.html",context=context) def update_student(request): student = Student.objects.get(pk= 2) student.s_name = 'pomelo' student.save() return HttpResponse("student update success") def delete_student(request): student = Student.objects.get(pk=3) student.delete() return HttpResponse("delete student success") 5.如果4中有些使用到了html,则需要在项目根目录下的templates下创建对应的html文件 如:student_list.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>StudentList</title> </head> <body> <h2>Ubuntu</h2> <h3>{{ hobby }}</h3> <h1>Student List</h1> <hr> <ul> {% for student in students %} <li>{{ student.s_name }}</li> {% endfor %} </ul> </body> </html>
render的原理,了解即可:
from django.http import HttpResponse from django.shortcuts import render # 其实是个快捷键 # Create your views here. from django.template import loader def index(request): three_index = loader.get_template('three_index.html') # 获取模板 result = three_index.render() # 渲染 print(result) return HttpResponse(result)
4.model-数据常规操作、级联数据
python manage.py shell:可以用来调试数据
D:\django_project\day01\HelloDjango>python manage.py shell Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from Two.models import Student >>> students = Student.objects.all() >>> students <QuerySet [<Student: Student object (1)>, <Student: Student object (2)>, <Student: Student object (3)>, <Student: Student object (4)>, <Student: Student object (5)>, <Student: Student ob ject (6)>, <Student: Student object (7)>]> >>> for student in students: ... print(student.s_name) ... Tom8 Tom97 Tom2 Tom45 Tom16 Tom30 Tom28
表关系:
1:1 给外键加上唯一约束
1:M 外键
M:N 额外创建一张表,记录另外两张表间的关系
踩坑是成长最快的方式