說明:本文環境windows+pycharm專業版html
1.HelloDjandopython
環境搭建:爲django項目建立本身獨立的python虛擬環境,並在pycharm中選擇解釋器爲剛剛建立的python虛擬環境mysql
進入到要建立django項目的目錄,執行以下命令sql
django-admin startproject HelloDjango #建立項目
python manage.py startapp App #建立應用,注意建立後先把該應用註冊到settings.py中,以避免後續忘記
python manage.py runserver #啓動服務
瀏覽器訪問:http://127.0.0.1:8000/ 可看到django的歡迎界面shell
2.MTV數據庫
模板配置:須要在項目目錄中建立templates文件夾並標記;須要在settings中進行註冊django
路由優化配置:拆分爲多個app;繼承拆分路由器windows
例子1:瀏覽器
瀏覽器訪問:http://127.0.0.1:8000/hello/app
例子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 額外建立一張表,記錄另外兩張表間的關係