按黑馬程式設計師的美多商場作方向:
https://www.bilibili.com/video/BV1nf4y1k7G3
一、應用建立、註冊處理、配置
Pycharm 建立Django專案:
自應用註冊處理:
二、應用資料初始化
第一步:建立後設資料初始化py指令碼
python manage.py makemigrations
初始化的指令碼會放在各個自應用的migrates目錄中
第二步:執行初始化py指令碼,把表結構資訊同步到資料庫中
sqlite不需要建庫即可實現,mysql需要先把model源資訊的庫建好才可以同步
python manage.py migrate
三、站點配置:
1、改成中文時區和語言編碼
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'
2、設定站點管理員賬號
必須要先透過初始化資料之後才能執行
python manage.py createsuperuser
更改密碼:
更改的金鑰要求8位長度,且至少有字母和數字組成
python manage.py changepassword 使用者名稱
3、配置自定義埠號:
#!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys from django.core.management.commands.runserver import Command as RunserverCommand def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjangoProject.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': RunserverCommand.default_port = 9090 main()
4、站點登陸:
http://127.0.0.1:9090/admin/
5、將模型資訊註冊到django-admin中:
重新整理頁面可以發現模型資訊可以直接在admin中進行管理
6、URL檢視配置
工程預設會綁上django-admin的url配置
http://127.0.0.1:9090/admin/
我們可以在這裡新增我們自己url配置,對應的,每個自應用也需要建立一個urls.py配置檔案
內容填寫,一個路由地址,對應檢視的一個方法:
""" URL configuration for DjangoProject project. The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/5.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include from .views import index urlpatterns = [ path('index/', index) ]
views.py中的index方法,呼叫的是httpResponse進行返回:
from django.http import HttpResponse # Create your views here. def index(request): return HttpResponse("Hello, world. You're at the index page.")
訪問頁面進行測試:
http://127.0.0.1:9090/book_manager/index/
7、使用模版渲染處理
建立模版檔案,templates為模版的路徑根目錄,也可以自己改成別的目錄
用自應用名稱區分開來,mustache語法來讀取模版引數
更改index方法邏輯:
from django.http import HttpResponse from django.shortcuts import render # Create your views here. def index(request): # return HttpResponse("Hello, world. You're at the index page.") context = {'title': 'Django Book Manager'} return render(request, 'book_manager/index.html', context)
重新整理檢視頁面:
8、上線配置ALLOW_HOSTS和關閉Debug模式
Debug模式將會把報錯資訊直接列印在頁面上
關閉Debug模式後,僅簡單輸出內容
9、靜態檔案管理
靜態檔案預設配置位置(settings檔案):
在Debug模式下可以直接訪問:
關閉Debug模式後不能訪問:
解決方案見此部落格:
https://blog.csdn.net/qq_41475058/article/details/105856148
1、設定STATIC_ROOT引數
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ # 靜態資源url訪問路徑 STATIC_URL = 'static/' # python manage.py collectstatic 收集後的資源目錄位置 STATIC_ROOT = os.path.join(BASE_DIR, 'static_prod')
2、給靜態目錄追加url對映配置:
""" URL configuration for DjangoProject project. The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/5.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include, re_path from django.views.static import serve from DjangoProject import settings urlpatterns = [ re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}, name='static'), path('admin/', admin.site.urls), path('book_manager/', include('book_manager.urls')), path('my_app/', include('my_app.urls')), path('my_app2/', include('my_app2.urls')), ]
3、執行collect命令,打包靜態資源
python manage.py collectstatic