溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Django中怎么建立mvc模式

發布時間:2021-04-28 13:56:00 來源:億速云 閱讀:189 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關Django中怎么建立mvc模式,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

1、說明

(1)models.py 文件主要用一個 Python 類來描畫數據表。 稱為模型(model) 。運用這個類(經過Django自帶的ORM完成),你能夠經過簡單的 Python 的代碼來創立、檢索、更新、刪除 數據庫中的記載。

(2)views.py文件包含了頁面的業務邏輯,latest_books()函數叫做視圖。

(3)urls.py指出了什么樣的URL調用什么的視圖。在這個例子中,/latest/URL將會調用latest_books()這個函數。換句話說,假定你的域名是example.com,任何人閱讀http://example.com/latest/將會調用latest_books()這個函數。

(4)latest_books.html 是 html 模板,它描畫了這個頁面的設計是如何的。 運用帶根本邏輯聲明的模板言語,如{% for book in book_list %}。

2、實例

下面是一個簡單的例子引見Django中的MVC(模型-視圖-控制器)設計方式:

# models.py (the database tables)
 
from django.db import models
 
class Book(models.Model):
    name = models.CharField(max_length=50)
    pub_date = models.DateField()
 
 
# views.py (the business logic)
 
from django.shortcuts import render_to_response
from models import Book
 
def latest_books(request):
    book_list = Book.objects.order_by('-pub_date')[:10]
    return render_to_response('latest_books.html', {'book_list': book_list})
 
 
# urls.py (the URL configuration)
 
from django.conf.urls.defaults import *
import views
 
urlpatterns = patterns('',
    (r'^latest/$', views.latest_books),
)
 
 
# latest_books.html (the template)
 
<html><head><title>Books</title></head>
<body>
<h2>Books</h2>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
</body></html>

關于“Django中怎么建立mvc模式”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女