django 傳遞中文
by Ogundipe Samuel
由Ogundipe Samuel
如何在Django中建立消息傳遞狀態 (How to Build a Message Delivery Status in Django)
Today, we will make a real-time message delivery status framework with Django and Pusher.
今天,我們將使用Django和Pusher建立一個實時消息傳遞狀態框架。
A basic understanding of Django and Vue is needed in order to follow this tutorial.
為了遵循本教程,需要對Django和Vue有基本的了解。
設置Django (Setting up Django)
First, we need to install the Python Django library if we don’t already have it. To install Django, we run:
首先,如果我們還沒有Python Django庫,則需要安裝它。 要安裝Django,我們運行:
After installing Django, it’s time to create our project. Open up a terminal and create a new project using the following command:
安裝Django之后,就該創建我們的項目了。 打開一個終端并使用以下命令創建一個新項目:
https://gist.github.com/4896cf41463ff83e191949a02bbead23
https://gist.github.com/4896cf41463ff83e191949a02bbead23
In the above command, we created a new project called pusher_message
. The next step will be to create an app inside our new project. To do that, let’s run the following commands:
在上面的命令中,我們創建了一個名為pusher_message
的新項目。 下一步將是在我們的新項目中創建一個應用程序。 為此,我們運行以下命令:
Once we are done setting up the new app, we need to tell Django about our new application, so we will go into our pusher_message\settings.py
and add the message app to our installed apps as seen below:
設置好新應用程序后,我們需要將新應用程序告訴Django,因此我們將進入pusher_message\settings.py
并將消息應用程序添加到已安裝的應用程序中,如下所示:
After doing the above, it’s time for us to run the application and see if all went well.
完成上述操作后,是時候運行該應用程序,看看一切是否正常了。
In our terminal shell, we run:
在終端外殼中,運行:
If we navigate our browser to http://localhost:8000
, we should see the following:
如果將瀏覽器導航到http://localhost:8000
,則應該看到以下內容:
在Pusher上設置應用 (Set up an App on Pusher)
At this point, Django is ready and set up. We now need to set up Pusher, as well as grab our app credentials.
至此,Django已準備就緒并準備就緒。 現在,我們需要設置Pusher,并獲取我們的應用程序憑據。
We need to sign up on Pusher, create a new app, and also copy our secret application key and application id.
我們需要注冊Pusher ,創建一個新應用,并復制我們的秘密應用密鑰和應用ID。
The next step is to install the required libraries:
下一步是安裝所需的庫:
In the above bash command, we installed one package, pusher
. This is the official Pusher library for Python, which we will be using to trigger and send our messages to Pusher.
在上面的bash命令中,我們安裝了一個程序包pusher
。 這是Python的官方Pusher庫,我們將使用它來觸發消息并將其發送到Pusher。
創建我們的應用程序 (Creating Our Application)
First, let us create a model class, which will generate our database structure.Let’s open up message\models.py
and replace the content with the following:
首先,讓我們創建一個模型類,該模型類將生成我們的數據庫結構。讓我們打開message\models.py
并將內容替換為以下內容:
In the above block of code, we defined a model called Conversation
. The conversation table consists of the following fields:
在上面的代碼塊中,我們定義了一個名為Conversation
的模型。 對話表包含以下字段:
- A field to link the message to the user who created it 將消息鏈接到創建消息的用戶的字段
- A field to store the message 用于存儲消息的字段
- A field to store the status of the message 用于存儲消息狀態的字段
- A filed to store the date and time the message was created 用于存儲消息創建日期和時間的文件
運行遷移 (Running Migrations)
We need to make migrations and also run them so our database table can be created. To do that, let us run the following in our terminal:
我們需要進行遷移并運行它們,以便可以創建數據庫表。 為此,讓我們在終端中運行以下命令:
創建我們的觀點 (Creating Our Views)
In Django, the views do not necessarily refer to the HTML structure of our application. In fact, we can see it as our Controller
, as referred to in some other frameworks.
在Django中,視圖不一定引用我們應用程序HTML結構。 實際上,我們可以將其視為其他框架中提到的Controller
。
Let us open up our views.py
in our message
folder and replace the content with the following:
讓我們在message
文件夾中打開views.py
并將內容替換為以下內容:
In the code above, we have defined four main functions which are:
在上面的代碼中,我們定義了四個主要功能,它們是:
index
index
broadcast
broadcast
conversation
conversation
delivered
delivered
In the index
function, we added the login required decorator, and we also passed the login URL argument which does not exist yet, as we will need to create it in the urls.py
file. Also, we rendered a default template called chat.html
that we will also create soon.
在index
函數中,我們添加了登錄所需的裝飾器,并且還傳遞了尚不存在的登錄URL參數,因為我們需要在urls.py
文件中創建它。 另外,我們渲染了一個默認模板chat.html
,我們還將很快創建它。
In the broadcast
function, we retrieved the content of the message being sent, saved it into our database, and finally triggered a Pusher request passing in our message dictionary, as well as a channel and event name. In the conversations
function, we simply grab all conversations and return them as a JSON response.
在broadcast
功能中,我們檢索了要發送的消息的內容,將其保存到我們的數據庫中,最后觸發了Pusher請求,該請求傳遞到消息字典中,以及通道和事件名稱。 在conversations
功能中,我們僅獲取所有對話并將其作為JSON響應返回。
Finally, we have the delivered
function, which is the function that takes care of our message delivery status.
最后,我們具有delivered
功能,該功能負責處理消息傳遞狀態。
In this function, we get the conversation by the ID supplied to us. We then verify that the user who wants to trigger the delivered event isn’t the user who sent the message in the first place. Also, we pass in the socket_id
so that Pusher does not broadcast the event back to the person who triggered it.
在此功能中,我們通過提供給我們的ID來獲取對話。 然后,我們驗證要觸發傳遞事件的用戶不是最初發送消息的用戶。 另外,我們傳入socket_id
以便Pusher不會將事件廣播回觸發它的人。
The socket_id
stands as an identifier for the socket connection that triggered the event.
socket_id
代表觸發事件的套接字連接的標識符。
填充URL的.py (Populating The URL’s.py)
Let us open up our pusher_message\urls.py
file and replace with the following:
讓我們打開我們的pusher_message\urls.py
文件,并替換為以下內容:
What has changed in this file? We have added six new routes to the file. We have defined the entry point and assigned it to our index
function. Next, we defined the login URL, which the login_required
decorator would try to access to authenticate users.
該文件有什么變化? 我們在文件中添加了6條新路線。 我們已經定義了入口點并將其分配給index
函數。 接下來,我們定義了登錄URL, login_required
裝飾器將嘗試使用該URL進行身份驗證用戶。
We have used the default auth
function to handle it but passed in our own custom template for login, which we will create soon.
我們使用默認的auth
函數來處理它,但傳入了我們自己的自定義模板進行登錄,該模板將很快創建。
Next, we defined the routes for the conversation
message trigger, all conversations
, and finally the delivered
conversation.
接下來,我們定義conversation
消息觸發器,所有conversations
以及最終delivered
對話的路由。
創建HTML文件 (Creating the HTML Files)
Now we will need to create two HTML pages so our application can run smoothly. We have referenced two HTML pages in the course of building the application.
現在,我們將需要創建兩個HTML頁面,以便我們的應用程序可以平穩運行。 在構建應用程序的過程中,我們引用了兩個HTML頁面。
Let us create a new folder in our messages
folder called templates
.
讓我們在messages
文件夾中創建一個名為templates
的新文件夾。
Next, we create a file called login.html
in our templates
folder and replace it with the following:
接下來,我們在templates
文件夾中創建一個名為login.html
文件,并將其替換為以下內容:
Vue組件和推桿綁定 (Vue Component And Pusher Bindings)
That’s it! Now, whenever a new message is delivered, it will be broadcast and we can listen, using our channel, to update the status in real-time. Below is our Example component written using Vue.js.
而已! 現在,每當有新消息傳遞時,都會廣播該消息,我們可以使用我們的頻道收聽實時更新狀態。 以下是我們使用Vue.js編寫的示例組件。
Please note: In the Vue component below, a new function called **queryParams**
was defined to serialize our POST body so it can be sent as **x-www-form-urlencoded**
to the server in place of as a **payload**
. We did this because Django cannot handle requests coming in as **payload**
.
請注意:在下面的Vue組件中,定義了一個名為**queryParams**
的新函數來序列化我們的POST正文,因此可以將它作為**x-www-form-urlencoded**
發送給服務器,而不是作為**payload**
。 我們這樣做是因為Django無法處理以**payload**
請求。
Below is the image demonstrating what we have built:
以下是展示我們所構建內容的圖像:
結論 (Conclusion)
In this article, we have covered how to create a real-time message delivery status using Django and Pusher. We have gone through exempting certain functions from CSRF checks, as well as exempting the broadcaster from receiving an event they triggered.
在本文中,我們介紹了如何使用Django和Pusher創建實時消息傳遞狀態。 我們已經免除了CSRF檢查中的某些功能,并且免除了廣播公司接收它們觸發的事件的機會。
The code is hosted on a public GitHub repository. You can download it for educational purposes.
該代碼托管在公共GitHub存儲庫上 。 您可以出于教育目的下載它。
Have a better way we could have built our application, reservations or comments? Let us know in the comments. Remember, sharing is learning.
有更好的方法來構建應用程序,保留或評論嗎? 讓我們在評論中知道。 記住,分享就是學習。
This post was originally published by the author in the pusher blog here.
該帖子最初是由作者在此處的pusher博客中發布的。
This version has been edited for clarity and may appear different from the original post.
為了清晰起見,已對該版本進行了編輯,該版本可能與原始帖子有所不同。
翻譯自: https://www.freecodecamp.org/news/how-to-build-a-message-delivery-status-in-django-e8d1eb2e8b6a/
django 傳遞中文