日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

django 传递中文_如何在Django中建立消息传递状态

發(fā)布時(shí)間:2023/11/29 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django 传递中文_如何在Django中建立消息传递状态 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

django 傳遞中文

by Ogundipe Samuel

由Ogundipe Samuel

如何在Django中建立消息傳遞狀態(tài) (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建立一個(gè)實(shí)時(shí)消息傳遞狀態(tài)框架。

A basic understanding of Django and Vue is needed in order to follow this tutorial.

為了遵循本教程,需要對(duì)Django和Vue有基本的了解。

設(shè)置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:

首先,如果我們還沒(méi)有Python Django庫(kù),則需要安裝它。 要安裝Django,我們運(yùn)行:

After installing Django, it’s time to create our project. Open up a terminal and create a new project using the following command:

安裝Django之后,就該創(chuàng)建我們的項(xiàng)目了。 打開(kāi)一個(gè)終端并使用以下命令創(chuàng)建一個(gè)新項(xiàng)目:

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:

在上面的命令中,我們創(chuàng)建了一個(gè)名為pusher_message的新項(xiàng)目。 下一步將是在我們的新項(xiàng)目中創(chuàng)建一個(gè)應(yīng)用程序。 為此,我們運(yùn)行以下命令:

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:

設(shè)置好新應(yīng)用程序后,我們需要將新應(yīng)用程序告訴Django,因此我們將進(jìn)入pusher_message\settings.py并將消息應(yīng)用程序添加到已安裝的應(yīng)用程序中,如下所示:

After doing the above, it’s time for us to run the application and see if all went well.

完成上述操作后,是時(shí)候運(yùn)行該應(yīng)用程序,看看一切是否正常了。

In our terminal shell, we run:

在終端外殼中,運(yùn)行:

If we navigate our browser to http://localhost:8000, we should see the following:

如果將瀏覽器導(dǎo)航到http://localhost:8000 ,則應(yīng)該看到以下內(nèi)容:

在Pusher上設(shè)置應(yīng)用 (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已準(zhǔn)備就緒并準(zhǔn)備就緒。 現(xiàn)在,我們需要設(shè)置Pusher,并獲取我們的應(yīng)用程序憑據(jù)。

We need to sign up on Pusher, create a new app, and also copy our secret application key and application id.

我們需要注冊(cè)Pusher ,創(chuàng)建一個(gè)新應(yīng)用,并復(fù)制我們的秘密應(yīng)用密鑰和應(yīng)用ID。

The next step is to install the required libraries:

下一步是安裝所需的庫(kù):

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命令中,我們安裝了一個(gè)程序包pusher 。 這是Python的官方Pusher庫(kù),我們將使用它來(lái)觸發(fā)消息并將其發(fā)送到Pusher。

創(chuàng)建我們的應(yīng)用程序 (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:

首先,讓我們創(chuàng)建一個(gè)模型類,該模型類將生成我們的數(shù)據(jù)庫(kù)結(jié)構(gòu)。讓我們打開(kāi)message\models.py并將內(nèi)容替換為以下內(nèi)容:

In the above block of code, we defined a model called Conversation. The conversation table consists of the following fields:

在上面的代碼塊中,我們定義了一個(gè)名為Conversation的模型。 對(duì)話表包含以下字段:

  • A field to link the message to the user who created it

    將消息鏈接到創(chuàng)建消息的用戶的字段
  • A field to store the message

    用于存儲(chǔ)消息的字段
  • A field to store the status of the message

    用于存儲(chǔ)消息狀態(tài)的字段
  • A filed to store the date and time the message was created

    用于存儲(chǔ)消息創(chuàng)建日期和時(shí)間的文件

運(yùn)行遷移 (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:

我們需要進(jìn)行遷移并運(yùn)行它們,以便可以創(chuàng)建數(shù)據(jù)庫(kù)表。 為此,讓我們?cè)诮K端中運(yùn)行以下命令:

創(chuàng)建我們的觀點(diǎn) (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中,視圖不一定引用我們應(yīng)用程序HTML結(jié)構(gòu)。 實(shí)際上,我們可以將其視為其他框架中提到的Controller 。

Let us open up our views.py in our message folder and replace the content with the following:

讓我們?cè)趍essage文件夾中打開(kāi)views.py并將內(nèi)容替換為以下內(nèi)容:

In the code above, we have defined four main functions which are:

在上面的代碼中,我們定義了四個(gè)主要功能,它們是:

  • 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函數(shù)中,我們添加了登錄所需的裝飾器,并且還傳遞了尚不存在的登錄URL參數(shù),因?yàn)槲覀冃枰趗rls.py文件中創(chuàng)建它。 另外,我們渲染了一個(gè)默認(rèn)模板chat.html ,我們還將很快創(chuàng)建它。

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功能中,我們檢索了要發(fā)送的消息的內(nèi)容,將其保存到我們的數(shù)據(jù)庫(kù)中,最后觸發(fā)了Pusher請(qǐng)求,該請(qǐng)求傳遞到消息字典中,以及通道和事件名稱。 在conversations功能中,我們僅獲取所有對(duì)話并將其作為JSON響應(yīng)返回。

Finally, we have the delivered function, which is the function that takes care of our message delivery status.

最后,我們具有delivered功能,該功能負(fù)責(zé)處理消息傳遞狀態(tài)。

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.

在此功能中,我們通過(guò)提供給我們的ID來(lái)獲取對(duì)話。 然后,我們驗(yàn)證要觸發(fā)傳遞事件的用戶不是最初發(fā)送消息的用戶。 另外,我們傳入socket_id以便Pusher不會(huì)將事件廣播回觸發(fā)它的人。

The socket_id stands as an identifier for the socket connection that triggered the event.

socket_id代表觸發(fā)事件的套接字連接的標(biāo)識(shí)符。

填充URL的.py (Populating The URL’s.py)

Let us open up our pusher_message\urls.py file and replace with the following:

讓我們打開(kāi)我們的pusher_message\urls.py文件,并替換為以下內(nèi)容:

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.

該文件有什么變化? 我們?cè)谖募刑砑恿?條新路線。 我們已經(jīng)定義了入口點(diǎn)并將其分配給index函數(shù)。 接下來(lái),我們定義了登錄URL, login_required裝飾器將嘗試使用該URL進(jìn)行身份驗(yàn)證用戶。

We have used the default auth function to handle it but passed in our own custom template for login, which we will create soon.

我們使用默認(rèn)的auth函數(shù)來(lái)處理它,但傳入了我們自己的自定義模板進(jìn)行登錄,該模板將很快創(chuàng)建。

Next, we defined the routes for the conversation message trigger, all conversations, and finally the delivered conversation.

接下來(lái),我們定義conversation消息觸發(fā)器,所有conversations以及最終delivered對(duì)話的路由。

創(chuàng)建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.

現(xiàn)在,我們將需要?jiǎng)?chuàng)建兩個(gè)HTML頁(yè)面,以便我們的應(yīng)用程序可以平穩(wěn)運(yùn)行。 在構(gòu)建應(yīng)用程序的過(guò)程中,我們引用了兩個(gè)HTML頁(yè)面。

Let us create a new folder in our messages folder called templates.

讓我們?cè)趍essages文件夾中創(chuàng)建一個(gè)名為templates的新文件夾。

Next, we create a file called login.html in our templates folder and replace it with the following:

接下來(lái),我們?cè)趖emplates文件夾中創(chuàng)建一個(gè)名為login.html文件,并將其替換為以下內(nèi)容:

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.

而已! 現(xiàn)在,每當(dāng)有新消息傳遞時(shí),都會(huì)廣播該消息,我們可以使用我們的頻道收聽(tīng)實(shí)時(shí)更新?tīng)顟B(tài)。 以下是我們使用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**.

請(qǐng)注意:在下面的Vue組件中,定義了一個(gè)名為**queryParams**的新函數(shù)來(lái)序列化我們的POST正文,因此可以將它作為**x-www-form-urlencoded**發(fā)送給服務(wù)器,而不是作為**payload** 。 我們這樣做是因?yàn)镈jango無(wú)法處理以**payload**請(qǐng)求。

Below is the image demonstrating what we have built:

以下是展示我們所構(gòu)建內(nèi)容的圖像:

結(jié)論 (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創(chuàng)建實(shí)時(shí)消息傳遞狀態(tài)。 我們已經(jīng)免除了CSRF檢查中的某些功能,并且免除了廣播公司接收它們觸發(fā)的事件的機(jī)會(huì)。

The code is hosted on a public GitHub repository. You can download it for educational purposes.

該代碼托管在公共GitHub存儲(chǔ)庫(kù)上 。 您可以出于教育目的下載它。

Have a better way we could have built our application, reservations or comments? Let us know in the comments. Remember, sharing is learning.

有更好的方法來(lái)構(gòu)建應(yīng)用程序,保留或評(píng)論嗎? 讓我們?cè)谠u(píng)論中知道。 記住,分享就是學(xué)習(xí)。

This post was originally published by the author in the pusher blog here.

該帖子最初是由作者在此處的pusher博客中發(fā)布的。

This version has been edited for clarity and may appear different from the original post.

為了清晰起見(jiàn),已對(duì)該版本進(jìn)行了編輯,該版本可能與原始帖子有所不同。

翻譯自: https://www.freecodecamp.org/news/how-to-build-a-message-delivery-status-in-django-e8d1eb2e8b6a/

django 傳遞中文

總結(jié)

以上是生活随笔為你收集整理的django 传递中文_如何在Django中建立消息传递状态的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。