使用pycharm創(chuàng)建django項(xiàng)目(2)
1、創(chuàng)建一個(gè)app,終端跳轉(zhuǎn)至新建django項(xiàng)目文件夾(帶有manage.py那個(gè)文件夾)
輸入:
python manage.py startapp app名稱? ?或??py?manage.py startapp app名稱
這會創(chuàng)建一個(gè)文件夾,目錄如下:
app名稱/
????__init__.py
????admin.py? ? ? ? ? ? ? ? 【django默認(rèn)提供admin后臺管理,可以用它管理model和數(shù)據(jù)庫】
????apps.py? ? ? ? ? ? ? ? ? 【app啟動類,django生成你所創(chuàng)建app名稱的文件】
????migrations/? ? ? ? ?【數(shù)據(jù)庫變更記錄,數(shù)據(jù)遷移包,負(fù)責(zé)遷移文件 , 生成數(shù)據(jù)庫表數(shù)據(jù)? ? ?__init__.py ? ? ? ? ? python?manage.py?makemigrations:生成遷移文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? python?manage.py?migrate:執(zhí)行遷移文件】
????models.py? ? ? ? ? 【對數(shù)據(jù)庫操作,數(shù)據(jù)庫表的映射】
????tests.py??????????????【測試文件,單元測試】
????views.py? ? ? ? ? ? ?【視圖函數(shù)文件】

2、注冊創(chuàng)建的app,打開settings.py文件,在 INSTALLED_APPS 里按格式添加你創(chuàng)建的app,可在app.py查看名稱


3、編寫url和視圖函數(shù)(urls.py、views.py)
在views.py中輸入以下代碼:
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
? ?return HttpResponse("歡迎使用小區(qū)出入門禁管理系統(tǒng)")

在urls.py中輸入以下代碼:
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
? ?https://docs.djangoproject.com/en/3.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
# 導(dǎo)入app視圖函數(shù)文件
from app01 import views
urlpatterns = [
? ?path('admin/', admin.site.urls),
# ? ? www.xxx.com/index/ ?->函數(shù)
? ?path('index/',views.index),
]

4、啟動項(xiàng)目,終端跳轉(zhuǎn)至新建django項(xiàng)目文件夾(帶有manage.py那個(gè)文件夾),并啟動項(xiàng)目?
輸入:py manage.py runserver

打開后會出現(xiàn)以下頁面:

在8080后加入/index即可訪問:
