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.mzph.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…