生成Django項(xiàng)目
- 現(xiàn)在cmd中使用命令安裝Django框架
pip install django==3.2
- 使用命令生成項(xiàng)目
django-admin startproject DjStore
- 使用命令生成應(yīng)用
python .\manage.py startapp news
python .\manage.py startapp users
在項(xiàng)目的setting文件中注冊(cè)
/DjStore/Djstore/setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"news",
"users"
]
編寫module
/DjStore/news/models.py
"""
新聞表:
ID:主鍵
title:標(biāo)題 字符串
content:新聞內(nèi)容 大文本
b_data:新聞日期 日期
read:閱讀量 整數(shù)
模型類:必須繼承 django.db.models.model類
"""
class NewsInfo(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
b_date = models.DateTimeField()
read = models.IntegerField()
- 執(zhí)行命名,生成mode
python .\manage.py makemigrations
Migrations for 'news':
news\migrations\0001_initial.py
- Create model NewsInfo
- 生成對(duì)應(yīng)的表結(jié)構(gòu)
python .\manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, news, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying news.0001_initial... OK
Applying sessions.0001_initial... OK
后臺(tái)管理系統(tǒng)admin
/Djstore/news/admin.py
from django.contrib import admin
from .models import NewsInfo
# Register your models here.
admin.site.register(NewsInfo)
- 啟動(dòng)項(xiàng)目
python .\manage.py runserver
- 生成admin賬號(hào)
python .\manage.py createsuperuser
account:qqg
password:123456
- 網(wǎng)址:http://127.0.0.1:8000/admin/
自定義管理頁(yè)面
from django.contrib import admin
from .models import NewsInfo
# Register your models here.
# style1:直接顯示
#admin.site.register(NewsInfo)
# style2:自定義顯示
class NewsInfoAdmin(admin.ModelAdmin):
list_display = ['id', 'title', 'b_date', 'read']
admin.site.register(NewsInfo, NewsInfoAdmin)
- 進(jìn)行對(duì)比
視圖函數(shù)使用
/DjStore/news/view.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
"""
視圖函數(shù)定義的基本要求:
1、視圖函數(shù)必須定義一個(gè)參數(shù)(通過(guò)命名為request)
request參數(shù):用來(lái)接受客戶端的請(qǐng)求信息的
2、視圖函數(shù)的返回值必須是一個(gè)HttpResponse的對(duì)象(或者HttpResponse的子類對(duì)象)
"""
def index(request):
res='this is a test'
return HttpResponse(res)
- 注冊(cè)u(píng)rl
/DjStore/DjStore/urls.py
from django.contrib import admin
from django.urls import path, include, re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^news/', include('news.urls'))
]
/DjStore/news/urls.py
from django.urls import path
from .views import index
# 配置路由規(guī)則
urlpatterns = [
path('index', index)
]
- 訪問(wèn)測(cè)試
- 使用測(cè)試
/DjStore/news/urls.py
from django.urls import path
from .views import index,news_list
# 配置路由規(guī)則
urlpatterns = [
path('index', index),
path('list', news_list)
]
/DjStore/news/views.py文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-601169.html
from django.shortcuts import render
from django.http import HttpResponse
from .models import NewsInfo
# Create your views here.
"""
視圖函數(shù)定義的基本要求:
1、視圖函數(shù)必須定義一個(gè)參數(shù)(通過(guò)命名為request)
request參數(shù):用來(lái)接受客戶端的請(qǐng)求信息的
2、視圖函數(shù)的返回值必須是一個(gè)HttpResponse的對(duì)象(或者HttpResponse的子類對(duì)象)
使用流程:
1、在應(yīng)用的views.py定義視圖函數(shù)
2、配置路由
1)、在項(xiàng)目日錄的UrLs,py中關(guān)聯(lián)應(yīng)用下的UrLs.py
from django.contrib import admin
from django.urls import path, include, re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^news/', include('news.urls'))
]
2)、在應(yīng)用的目錄下定義一個(gè)Urls.py文件(可以直接copy項(xiàng)目目錄下的urls.py進(jìn)來(lái))
3)、在應(yīng)用的UrLs.py配置具體的訪問(wèn)規(guī)則
from django.urls import path
from .views import index
# 配置路由規(guī)則
urlpatterns = [
# http://域名(ip:端口)/news/index
path('index', index)
]
"""
def index(request):
res = 'this is a test'
return HttpResponse(res)
def news_list(request):
datas = NewsInfo.objects.all()
result = ''
for item in datas:
title = '<h1>{}</h1>'.format(item.title)
result += title
return HttpResponse(result)
Django模板
- 創(chuàng)建目錄/templates/news與/templates/users
- 在/DjStore/DjStore/setting.py中設(shè)置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 項(xiàng)目模板的路徑
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
- /templates/news下,創(chuàng)建list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新聞列表頁(yè)面</title>
</head>
<body>
<h1 style="color: red">新聞列表頁(yè)面</h1>
<ul>
<li>python</li>
<li>java</li>
<li>js</li>
</ul>
</body>
</html>
- /DjStore/news/urls.py
from django.urls import path
from .views import index, news_list, list2
# 配置路由規(guī)則
urlpatterns = [
path('index', index),
path('list', news_list),
path('list2', list2)
]
- /DjStore/news/views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import NewsInfo
# Create your views here.
"""
def index(request):
res = 'this is a test'
return HttpResponse(res)
def news_list(request):
datas = NewsInfo.objects.all()
result = ''
for item in datas:
title = '<h1>{}</h1>'.format(item.title)
result += title
return HttpResponse(result)
# 視圖中使用模板文件
def list2(request):
return render(request, 'news/list.html')
-
訪問(wèn)測(cè)試
-
模板配置和使用規(guī)則
- 在項(xiàng)目目錄下創(chuàng)建一個(gè)templates文件夾
- 在setting.py中TEMPLATES:選項(xiàng)中配置項(xiàng)目模板的根路徑
‘DIRS’[BASE_DIR ‘templates’] - 在templates中創(chuàng)建和應(yīng)用同名的文件夾
- 在templates下應(yīng)用同名的文件夾中創(chuàng)建html模板頁(yè)面
- 在views.py中定義視圖函數(shù),并返回html模板頁(yè)面
- 配置路由訪問(wèn)規(guī)則
-
模板使用
-
/DjStore/news/views.py
# 視圖中使用模板文件
def list2(request):
datas = NewsInfo.objects.all()
item = datas[0]
info = {
"title": item.title,
"content": item.content,
"b_date": item.b_date,
"read": item.read
}
return render(request, 'news/list.html', info)
- /templates/news/list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新聞列表頁(yè)面</title>
</head>
<body>
<h1 style="color: red">{{ title }}</h1>
<h4>發(fā)布日期:{{ b_date }},閱讀量:{{ read }}</h4>
<pre>{{ content }}</pre>
</body>
</html>
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-601169.html
到了這里,關(guān)于Django項(xiàng)目開(kāi)發(fā)快速入門的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!