django构建网页

by Ogundipe Samuel

由Ogundipe Samuel

如何使用Django构建照片供稿 (How to build a photo feed using Django)

Today, we will make a real-time photo feed framework using Django and Pusher. This is like a mini Instagram, but without the comments and filter functionality.

今天,我们将使用Django和Pusher创建一个实时照片提要框架。 这就像一个迷你Instagram,但没有评论和过滤功能。

A basic understanding of Django and jQuery is needed to follow this tutorial.

要学习本教程,需要对Django和jQuery有基本的了解。

设置Django (Setting up Django)

First, you need to install the Django library (if we don’t have it).

首先,您需要安装Django库(如果我们没有)。

To install Django, we run:

要安装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之后,就该创建我们的项目了。 打开一个终端并使用以下命令创建一个新项目:

In the above command, we created a new project called photofeed. The next step will be to create an app inside our new project. To do that, let’s run the following commands:

在上面的命令中,我们创建了一个名为photofeed的新项目。 下一步将是在我们的新项目中创建一个应用程序。 为此,我们运行以下命令:

Once we’re done setting up the new app, Django needs to know about our new application. To do this, we will go into our feed\settings.py and add the message app to our installed apps as seen below:

一旦我们完成了新应用的设置,Django就需要了解我们的新应用。 为此,我们将进入feed\settings.py并将消息应用添加到已安装的应用中,如下所示:

After doing the above, it’s time 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 need to set up Pusher next, as well as grab our app credentials. If you haven’t already, sign up to a free Pusher account and create a new app, then copy your secret, application key and application id.

至此,Django已准备就绪并准备就绪。 接下来,我们需要设置Pusher,并获取我们的应用程序凭据。 如果还没有,请注册一个免费的Pusher帐户并创建一个新应用,然后复制您的秘密,应用密钥和应用ID。

The next step is to install the required libraries:

下一步是安装所需的库:

In the above bash command, we installed one package, Pusher. — Pusher: This is the official Pusher library for Python. We will be using this library to trigger and send our messages to the Pusher HTTP API.

在上面的bash命令中,我们安装了一个程序包Pusher。 — Pusher:这是Python的官方Pusher库。 我们将使用该库来触发消息并将其发送到Pusher HTTP API。

创建我们的应用程序 (Creating Our Application)

First, let us create a model class, which will generate our database structure. Let’s open up feed\models.py and replace with the following:

首先,让我们创建一个模型类,该类将生成我们的数据库结构。 让我们打开feed\models.py并替换为以下内容:

In the above block of code, we defined a model called Feed. The Feed table will consist of the following fields:

在上面的代码块中,我们定义了一个名为Feed的模型。 Feed表将包含以下字段:

  • A field to store the description of the photo用于存储照片描述的字段
  • A field to store the photo In the above code, while declaring our document field, we have included an upload_to attribute, which we set to static/documents

    存储照片的字段在上面的代码中,在声明文档字段时,我们包含了一个upload_to属性,我们将其设置为static/documents

    Please note that this path is relative to the path of the

    请注意,此路径是相对于

    DJANGO MEDIA ROOT, which we will set now.

    DJANGO MEDIA ROOT ,我们现在设置。

While in this article, we will be setting the MEDIA_ROOT to the static folder in our feed app, so it can get served as a static file. To do that, let us move to our photofeed/settings.py and add the code below to our file, immediately after the STATIC_URL declaration.

在本文中,我们将在feed应用程序中将MEDIA_ROOT设置为静态文件夹,以便可以将其用作静态文件。 为此,让我们移至photofeed/settings.py并在STATIC_URL声明之后立即将以下代码添加到我们的文件中。

运行迁移 (Running Migrations)

We need to make migrations and run them, so our database table can get created. To do that, let us run the following in our terminal:

我们需要进行迁移并运行它们,以便可以创建数据库表。 为此,让我们在终端中运行以下命令:

创建我们的观点 (Creating Our Views)

Our views refer to the file/files that hold up the logic behind the application, often referred to as the Controller. Let us open up our views.py in our feed folder and replace with the following:

我们的视图指向的是支撑应用程序背后逻辑的文件(通常称为Controller 。 让我们在feed文件夹中打开views.py并替换为以下内容:

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

在上面的代码中,我们定义了三个主要功能:

  • index指数
  • pusher_authentication_pusher_authentication_
  • push_feedpush_feed

In the index function, we fetch all the available photos in the database. The photos are then rendered in the view. This enables a new user to see all previous feeds that are available.

index功能中,我们获取数据库中所有可用的照片。 然后在视图中渲染照片。 这样,新用户就可以查看所有先前可用的提要。

In the pusher_authentication function, we verify that the current user can access our private channel.

pusher_authentication函数中,我们验证当前用户可以访问我们的私有频道。

In the push_feed function, we check if it is a POST request, then we try validating our form before saving it into the database. (The form used in this method named DocumentForm is not available yet. We will be creating it soon.) After the form validation, we then place our call to the Pusher library for real-time interaction.

push_feed函数中,我们检查它是否为POST请求,然后在将表单保存到数据库之前尝试对其进行验证。 (此方法中使用的名为DocumentForm的表单尚不可用。我们将很快创建它。)在表单验证之后,我们将调用放置到Pusher库中进行实时交互。

创建表单类 (Creating The Form Class)

A Django Form handles taking user input, validating it, and turning it into Python objects. They also have some handy rendering methods. Let us create a file called forms.py in our feed folder and add the following content to it:

Django Form处理用户输入,验证输入并将其转换为Python对象。 他们也有一些方便的渲染方法。 让我们在feed文件夹中创建一个名为forms.py文件,并向其中添加以下内容:

In the above code block, we have imported our Feed model and used it to create a form. This form will now handle the validation and upload of images to the right folder.

在上面的代码块中,我们导入了Feed模型并将其用于创建表单。 现在,此表单将处理验证并将图像上传到正确的文件夹。

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

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

让我们打开我们的photofeed\urls.py文件,并替换为以下内容:

What has changed in this file? We have added 3 new routes to the file. We have defined the entry point, and have assigned it to our index function. We also defined the push_feed URL and assigned it to our push_feed function. This will be responsible for pushing updates to Pusher in real-time. Finally, the pusher_authentication endpoint handles the authentication of our private channel.

该文件有什么变化? 我们在文件中添加了3条新路线。 我们已经定义了入口点,并将其分配给index函数。 我们还定义了push_feed URL,并将其分配给我们的push_feed函数。 这将负责实时将更新推送到Pusher。 最后, pusher_authentication端点处理我们的专用通道的身份验证。

创建HTML文件 (Creating the HTML Files)

Now we need to create the index.html file which we have referenced as the template for our index function. Let us create a new folder in our feed folder called templates. Next, we create a file called index.html in our templates folder and replace it with the code below:

现在,我们需要创建index.html文件,该文件已作为索引函数的模板被引用。 让我们在feed文件夹中创建一个名为templates的新文件夹。 接下来,我们在templates文件夹中创建一个名为index.html文件,并将其替换为以下代码:

In this HTML snippet, note that we have included some required libraries such as:

在此HTML代码段中,请注意,我们包含了一些必需的库,例如:

  • Bootstrap CSS引导CSS
  • jQuery JavaScript libraryjQuery JavaScript库
  • Pusher JavaScript libraryPusher JavaScript库

Pusher绑定和jQuery代码段 (Pusher Bindings And jQuery Snippet)

That’s it! Now, once a photo gets uploaded, it also gets broadcast and we can listen using our channel to update the feed in real-time. Below is our example jQuery snippet used to handle the file upload as well as Pusher’s real-time updates.

而已! 现在,一旦照片被上传,它也将被广播,我们可以使用我们的频道收听实时更新供稿。 以下是我们的示例jQuery代码段,用于处理文件上传以及Pusher的实时更新。

Below is an image of what we have built:

下面是我们构建的图像:

结论 (Conclusion)

In this article, we have covered how to create a real-time photo feed using Django and Pusher as well as passing CSRF tokens in AJAX request using Django.

在本文中,我们介绍了如何使用Django和Pusher创建实时照片供稿,以及如何使用Django在AJAX请求中传递CSRF令牌。

The code base to this tutorial is available in 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 on Pusher’s blog here

这篇文章最初发表在推的博客在这里

翻译自: https://www.freecodecamp.org/news/how-to-build-a-photo-feed-using-django-2d81c8519594/

django构建网页

django构建网页_如何使用Django构建照片供稿相关推荐

  1. django构建网页_通过解决问题的方式学习django,律师如何构建副业

    django构建网页 This post was originally published on Codementor's blog 该帖子最初发布在 Codementor的博客上 "I'm ...

  2. python django 动态网页_使用Django创建动态页面

    将 URL 映射到视图 那么概括起来,该视图函数返回了包含当前日期和时间的一段 HTML 页面.但是如何告诉 Django 使用这段代码呢?这就是 URLconfs 粉墨登场的地方了. URLconf ...

  3. java构建网页_从网页搭建入门Java Web2018版

    步骤1: 网页搭建入门 本步骤将学习到搭建网页的所需的HTML.CSS和JavaScript等内容.并完成个人生活记录首页和轮播图效果.最终达到独立完成前端页面开发的目的. 第1课 HTML入门 本堂 ...

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

    django 传递中文 by Ogundipe Samuel 由Ogundipe Samuel 如何在Django中建立消息传递状态 (How to Build a Message Delivery ...

  5. django模型查询_如何在Django中编写有效的视图,模型和查询

    django模型查询 I like Django. It's a well-considered and intuitive framework with a name I can pronounce ...

  6. python的django看不懂_学Python Django学得很迷茫,怎么办?

    不请自来... 因为最近python有点"过火",所以作为一个新时代的新青年,也得学一下吧!于是乎我就开始了自学之路. 先说下自己目前的心路历程,再说下我对题主遇到的问题的看法和建 ...

  7. 一直在构建工作空间_基于用户场景构建的建筑工程弱电设计工作设想

    [摘要]因为弱电产品更新速度快,功能差异变化大,往往会出现设计成果同预期有所区别的情况.针对类似情形,文章提出借鉴发展变化更加迅速的互联网行业中产品设计的理念,通过业主方或者设计方构建用户场景的手段, ...

  8. qt构建浏览器_如何为组织构建安全的浏览器

    qt构建浏览器 The most vulnerable part of most organization's network infrastructure is their browser. Thi ...

  9. python django廖雪峰_如何用Django从零开始搭建一个网站(0)

    python,django等安装就直接略过了.下面直接奔如主题,搭建网站. Step1:新建一个django project,运行命令:'django-admin startproject myPit ...

最新文章

  1. C语言笔记(关键字)
  2. Harbor升级和数据库迁移手册
  3. python中的作用域_python 模块的作用域
  4. Netty工作笔记0009---Channel基本介绍
  5. 【深度优先搜索】一个实例+两张动图彻底理解 DFS | DFS 与 BFS 的区别 | 用 DFS 自动控制我们的小游戏
  6. 长连接测试_电磁兼容测试照片
  7. Java、JSP员工考勤管理系统
  8. 黑科技 | 电脑必备黑科技软件
  9. C# 多线程BackgroundWorker
  10. app漏洞扫描原理_APP漏洞如何检测,如何检测出app有漏洞?
  11. 使用Flash地图控件AnyMap创建Self Drilldown Maps
  12. S60 Python 编程指南——如何创建pys60应用程序
  13. 八个步骤实现一个Web项目(在线聊天室)
  14. python处理点云数据_python将指定点云文件(asc)转换为PCD格式
  15. IDEA项目包的导入以及压缩包的快速导出
  16. 2022年第四届河南省CCPC大学生程序设计竞赛代码+简单思路(退役战了算是,还好金了)
  17. 《数据结构与算法》(十九)- 多路查找树
  18. 2021-2027全球与中国射频屏蔽窗市场现状及未来发展趋势
  19. 年轻人,请听我说……
  20. 面试时衣服出汗了怎么办

热门文章

  1. quartz.net 执行后台任务
  2. [deviceone开发]-do_Album的简单示例
  3. jmeter学习笔记(一)
  4. 亲历腾讯WEB前端开发三轮面试经历及面试题
  5. WPF疑难杂症之二(全屏幕窗口)
  6. spring boot redis 分布式锁
  7. sqldeveloper的查看执行计划快捷键F10
  8. 类的转换函数调用的优先级与是否用const修饰的关系
  9. 分布式定时任务框架Elastic-Job的使用
  10. CodeForces 696B Puzzles