国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Django項(xiàng)目開(kāi)發(fā)快速入門

這篇具有很好參考價(jià)值的文章主要介紹了Django項(xiàng)目開(kāi)發(fā)快速入門。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

生成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/
    Django項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端

自定義管理頁(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ì)比
    Django項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端
    Django項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端

視圖函數(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è)試
    Django項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端
  • 使用測(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

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項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端

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è)試
    Django項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端

  • 模板配置和使用規(guī)則

    1. 在項(xiàng)目目錄下創(chuàng)建一個(gè)templates文件夾
    2. 在setting.py中TEMPLATES:選項(xiàng)中配置項(xiàng)目模板的根路徑
      ‘DIRS’[BASE_DIR ‘templates’]
    3. 在templates中創(chuàng)建和應(yīng)用同名的文件夾
    4. 在templates下應(yīng)用同名的文件夾中創(chuàng)建html模板頁(yè)面
    5. 在views.py中定義視圖函數(shù),并返回html模板頁(yè)面
    6. 配置路由訪問(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>

Django項(xiàng)目開(kāi)發(fā)快速入門,Django項(xiàng)目開(kāi)發(fā),數(shù)據(jù)庫(kù),django,sqlite,python,后端文章來(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包