一、Django開發環境搭建:
1.安裝python:django可運行于版本python 2.7、3.x
2.安裝相應的IDE
3.安裝pip:sudo apt-get install python-pip(linux為例)
4.安裝django:1)pip安裝:sudo pip install Django(linux為例)
? ? ? ? ? ? ? ? ? ? ? ? ?2)源碼安裝:網上找教程
5.建立django項目:django-admin startproject projectname(這里可以選擇自己要建立項目的文件夾)
二、Django創建工程及應用
1.工程目錄詳解
? ? ?manage.py:管理項目---包括數據庫建立、服務器運行、測試。。。
? ? ?mysite目錄:
? ? ? ? ? ? ? ?settings.py:配置文件:應用。中間件、數據庫、靜態目錄等。。
? ? ? ? ? ? ? ?urls.py:URL映射配置文件:決定一個url訪問被哪個程序(函數)調用。。
? ? ? ? ? ? ? ?wsgi.py:python應用程序或框架和web服務器之間的接口
2.創建應用(django中使用應用來分割功能)
? ? ?2.1創建應用blog:$python manage.py startapp blog
? ? ?2.2添加blog應用:mysite/setting.py->INSTALLED_APPS添加相應的應用名即可(我這里是bikeFauleDia)
? ? ? ? ? ? ? ? ? ? ?
3.應用目錄詳解:
? ? ?views.py:相應邏輯函數用來跳轉頁面或功能處理,即相應客戶http請求,進行邏輯處理,返回給用戶html頁面
? ? ?models.py:映射數據庫中的表
? ? ?admin.oy:admin相關操作
? ? ?test.py:測試相關
? ? ?templates:用來成才html頁面。返回給用戶html,是由數據(模型)和模版渲染出來的
4、http請求響應過程
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
5、數據庫連接:
? ?1、在settings.py
文件中設置默認連接數據庫的方式(注意settings.py
頭部加編碼)
DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql', #數據庫類型'NAME': 'bikeData', #數據庫名字'USER': 'root', #用戶名'PASSWORD': 'helloworld', #密碼'HOST': '127.0.0.1', #服務器地址'PORT': '3306', #端口}
}
? ? 2、在組件(App
)目錄下面都有一個models.py
來寫本組件(App
)的數據模型(以創建應用用戶名和密碼為例)
from django.db import models
# Create your models here.class User(models.Model):username = models.CharField(max_length=50)password = models.CharField(max_length=50)
? ?3、創建映射文件:python manage.py makemigrations 組件名稱
? ?4、將映射文件中的映射到數據庫中:python manage.py migrate 組件名稱
補充:
一、編寫Models
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
二、設置html中css/js等外鏈樣式的時候:
? ? ?首先在head中加載? ? ? {% load staticfiles %}
? ? ?將css/js文件存放如static文件包內
? ? ?然后將所要鏈接的對象改寫成django格式:? ?<link rel="stylesheet" href="{% static 'css/base.css' %}">
Django中頁面超鏈接跳轉:
? ? ? ? ? ? ? ? ? ? ??
最后一個param是參數,有就傳,沒有就不穿。
三、django實現簡單的登錄驗證
views.py中:
from django.shortcuts import render
# from django.http import HttpResponse# from django import forms
from bikeFauleDia.models import User
# from django.http.response import HttpResponseRedirect, JsonResponse
from functools import wraps# Create your views here.#check_login用來判斷是否登錄過,并一解釋器的方式過濾頁面
def check_login(f):@wraps(f)def inner(request,*arg,**kwargs):if request.session.get('is_logion')=='1':return f(request,*arg,**kwargs)else:return render(request,'BikeFaultDiagnosis/login.html')return innerdef login(request):print(request.method)
# labers=Falseif request.method=='POST':username=request.POST.get('username') #獲取頁面用戶名信息password=request.POST.get('password')user=User.objects.filter(username=username,password=password) #和數據庫中用戶信息對比print(user)if user:labers=Falserequest.session['is_logion']='1' #設置session信息用來驗證登錄情況request.session['user_id']=user[0].idreturn render(request,'BikeFaultDiagnosis/index.html',{'labers':labers})else:labers=Truereturn render(request,'BikeFaultDiagnosis/login.html',{'labers':labers})return render(request,'BikeFaultDiagnosis/login.html')#=================================#========================================@check_login #裝飾器,用來驗證是否登錄
def index(request):return render(request,'BikeFaultDiagnosis/index.html')
urls.py中:
from django.urls import pathfrom . import views
app_name='bikeFauleDia'urlpatterns = [path('index/', views.index,name='index'),path('carContrl/', views.carContrl,name='carContrl'),path('mapp/', views.mapp,name='mapp'),path('static/', views.static,name='static'),path('table1/', views.table1,name='table1'),path('message/', views.message,name='message'),path('login/', views.login,name='login'),
]
?