django 傳遞中文_如何在Django中建立消息傳遞狀態

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 傳遞中文

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394860.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394860.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394860.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

軟鏈接與硬鏈接

文件引用模型 在linux中,一切皆文件,而文件包含元數據(metedata)和用戶數據(user data)。元數據中的inode號是系統標識和獲取用戶數據的唯一憑證,而文件名僅是為了方便用戶記憶和使用。為了管理…

c++ 數組的輸入遇到特定字符停止輸入_C語言 第4章-字符串和格式化輸入/輸出

#include 用數組name儲存字符串,name數組有40個字節,每個字節儲存一個字符值。在scanf()函數中,輸入字符串name沒有&前綴。C預處理器把字符常量DENSITY定義為62.4。strlen()獲取字符串長度。1. 字符串1.1. char類型數組雙引號標記字符串&…

vue3+typescript引入外部文件

vue3typescript中引入外部文件有幾種方法 &#xff08;eg:引入echarts&#xff09; 第一種方法&#xff1a; 1 indext.html中用script引入 <div id"app"></div><script src"https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.min.js…

在哪能找到陌生人聊騷_如何說服陌生人幫助您找到工作

在哪能找到陌生人聊騷by Alex Lacey通過亞歷克斯萊西 找工作嗎&#xff1f; 這是說服陌生人幫助您找到一個人的方法 (Looking for a job? Here’s how to convince strangers to help you find one) 我過去獲得40個推薦的7個步驟 (The 7-step process that I used to get 40 …

Python基礎 day2

Python基礎 一、Python的數據類型 1、int(整型) 在32位機器上&#xff0c;整數的位數為32位&#xff0c;取值范圍為-2**31&#xff5e;2**31-1&#xff0c;即-2147483648&#xff5e;2147483647&#xff0c;而.在64位系統上&#xff0c;整數的位數為64位&#xff0c;取值范圍為…

matlab 文件指針回到開頭,[c/c++] 文件指針位置回到文件開頭(rewind)及行開頭(ftell+fseek)...

待讀入文件1.greenteemo2.csdn3.blog代碼&#xff0c;詳細說明見注釋#include #define LENGTH_OF_LINE 1024int main(){FILE *fp fopen("file.txt", "r"); // 打開文件char line[LENGTH_OF_LINE];while( fgets(line, LENGTH_OF_LINE, fp) ){printf("…

python全棧開發優勢_Python全棧開發多少錢?學Python價格貴嗎?

Python全棧開發培訓多少錢?學習Python是大家進入編程世界的理想之選&#xff0c;而且Python也是一門非常受歡迎的編程&#xff0c;可以從事的領域有很多。 從目前市場上的行情來說&#xff0c;一般情況下Python培訓的費用在一萬五到兩萬元之間的&#xff0c;以后可能會更高&am…

gym101808 E

提問&#xff1a;我是什么品種的傻逼&#xff1f; 哇看到積水興高采烈啊。然后就走上了一條不歸路。 為什么不歸呢&#xff0c;因為我這個法子就是不對的&#xff0c;我總是在想很多很多點圍成的一塊區域&#xff0c;然后求這一塊區域的面積。 然后嘗試了各種掃描方法&#xff…

WordPress中纏結的自定義數據世界

by Kamil Grzegorczyk通過卡米爾(Kamil Grzegorczyk) WordPress中纏結的自定義數據世界 (The Tangled World of Custom Data in WordPress) 降低風險并管理您的自定義字段 (Reducing Risk and Managing Your Custom Fields) Have you ever wondered how to properly name keys…

【站點部署】解析二級域名并部署站點

開設原因 : 近期在學健身, 上一份工作辭掉后, 在北京找了家私人教練培訓學校, 進行為期四個月的健身培訓, 這個比在健身房找私教專業多了, 被健身房私人教練坑慘了, 說多了都是淚, 已經培訓了將近一個半月, 學習了基礎私教, 普拉提, 這在學習康復課程, 之后還有功能性 和 綜合格…

pip如何安裝到Linux服務器,linux中pip安裝步驟與使用詳解

1、pip下載安裝1.1 pip下載代碼如下# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5834b2904f92d46aaa333267fb1c922bb" --no-check-certificate1.2 pip安裝代碼如下# tar -xzvf pip-1.5.4.tar.gz# cd pip-1.5.4# python setup.py inst…

python中列表實現去重使用_Python實現嵌套列表去重方法示例

發現問題 python嵌套列表大家應該都不陌生&#xff0c;但最近遇到了一個問題&#xff0c;這是工作中遇到的一個坑&#xff0c;首先看一下問題 raw_list [["百度", "CPY"], ["京東", "CPY"], ["黃軒", "PN"], [&q…

Android 開發 存儲目錄的詳解

Android 開發 存儲目錄的詳解 簡介   Android設備,有3個地方的文件存儲位置,他們分別是:  內部存儲空間(用戶無法瀏覽到此目錄)  外部存儲空間(就是手機自身的文件管理目錄,用戶可以瀏覽)  SD卡的存儲空間(需要插入T卡)  SharedPreferences目錄  存儲數據庫DB目錄內…

大數據項目交付國標_在緊迫的期限內交付大型項目

大數據項目交付國標by Paul McGillivray保羅麥吉里夫瑞(Paul McGillivray) 在緊迫的期限內交付大型Web項目 (Delivering a big web project for a tight deadline) This week we launched the first phase of a large website for a fast-growing business, ‘Jump In’. The …

CentOS 安裝MySQL(rpm)提示錯誤Header V3 DSA/SHA1 Signature

提示錯誤&#xff1a;Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY error: Failed dependencies 錯誤原因&#xff1a;這是由于yum安裝了舊版本的GPG keys造成的 解決辦法&#xff1a;后面加上--force --nodeps 原文&#xff1a; 摘要&#xff1a; CentOS安裝rpm安裝…

linux系統文件的復制,linux操作系統文件復制操作

《linux操作系統文件復制操作》由會員分享&#xff0c;可在線閱讀&#xff0c;更多相關《linux操作系統文件復制操作(5頁珍藏版)》請在人人文庫網上搜索。1、網絡操作系統”課程實驗報告名:號:業:計算機科學與技術間:2012年5月 日師:北京聯合大學-信息學院編制Linux-文件的系統…

惡意軟件偽裝“正規軍”,撕開Booster Cleaner“畫皮”下的真相

經常使用手機瀏覽器閱讀小說的用戶都知道&#xff0c;在瀏覽器頁面經常會出現一些推廣游戲應用、手機清理應用等應用的彈窗廣告。有時出于方便&#xff0c;我們也會選擇直接點開這些彈窗廣告進行應用下載。但這種行為并不安全&#xff0c;部分惡意應用會先偽裝成“正規軍”誘導…

python初學者代碼示例_python基礎示例

7、寫代碼 &#xff08;1&#xff09;實現用戶輸入用戶名和密碼,當用戶名為 seven 且 密碼為 123 時,顯示登陸成功,否則登陸失敗!_name "seven"_pwd "123"name input("username:").strip() pwd input("password:").strip()if name _…

33歲想從頭學做網頁設計_從頭開始設計精美的移動應用

33歲想從頭學做網頁設計by Harshita Arora通過Harshita Arora 從頭開始設計精美的移動應用 (Designing beautiful mobile apps from scratch) I started learning graphic design when I was 13. I learned to design websites from online courses and used to play around w…

Lucene 基礎理論 (zhuan)

http://www.blogjava.net/hoojo/archive/2012/09/06/387140.html**************************************** 1. 全文檢索系統與Lucene簡介 1.1 什么是全文檢索與全文檢索系統 全文檢索是指計算機索引程序通過掃描文章中的每一個詞&#xff0c;對每一個詞建立一個索引&#xff0…