Django-在项目中一起使用不同的应用程序 [英] Django - using different apps together in a project

查看:133
本文介绍了Django-在项目中一起使用不同的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,在我发现的基本Django教程和文档中,应用程序被视为项目的某些独立部分。
不过我还没有找到在复杂项目中一起使用它们的解决方案。

In the basic Django tutorials and documentations I have found so far, apps are treated as some standalone parts of the project. I haven't yet found, though, solutions of using them together in a complex project.

我们可以在域/注册,由 users.views.signup 处理。


  • 对于GET请求,注册呈现 signup.html

  • 对于POST请求,在评估发布的数据之后,它要么
    渲染 signup.html 并显示一些消息,要么注册一个新用户
    登录他/她并重定向到某处。

  • For a GET request signup renders signup.html.
  • For a POST request, after evaluating the posted data, it either renders signup.html with some message or registers a new user, logs him/her in and redirects somewhere.

我们可以在 domain / new_post创建新帖子(或 domain / user / new_post ),由 posts.views.new_post 。它的作用类似于 signup 处理程序:

We can create a new post at domain/new_post (or domain/user/new_post), which is handled by posts.views.new_post. It acts similarly to the signup handler:


  • 对于GET请求,它会呈现 new_post.html

  • 对于POST请求,它会评估发布数据,然后呈现
    new_post。带有消息的html 或注册新帖子,并将
    重定向到某处。

  • For a GET request it renders new_post.html.
  • For POST requests it evaluates post data and then either renders new_post.html with messages or registers the new post and redirects somewhere.



普通网站由几个应用程序组成,显示在某个网址上的请求网页提供了多个应用程序的功能。

例如,登录到一些受欢迎的社交网站后,我们可以创建新的条目,搜索用户(通常是更广泛的搜索,但是为了简单起见,我们只处理用户),广告和我们的朋友也可能会在侧边栏中列出。


A general website is built up of several apps, and the webpage displayed for a request at some url offers functionalities from several apps.
For example, after logging into some popular social site, we can create new entries on our wall, search users (it is normally a wider search, but for sake of simplicity, let's only deal with users), also ads and our friends may be listed in the sidebars.

如何从不同应用程序的不同视图和模板构建这些部分?

要使问题更具体:

How are these parts built up from the different views and templates of the different apps?
To make the question more specific:


  • 对于在以下位置收到的GET请求一些网址,我们如何才能将
    用户搜索表单,new-post表单和friends-list一起显示?

    如果我们通过某些项目级别的通用视图来做到这一点,那应用级视图?

    我已经读到 {%include%} 标记对于从多个子模板构建模板很方便,但是

  • 我们如何处理与用户搜索相关的GET请求以及可能从同一网址发送的与新帖子相关的POST请求?
    >
    (从网址发送的请求这个表达可能不合适,但是用户可以通过对显示在一个特定网址上的页面进行操作来发送不同的请求。)

  • 也,不同的组件应保持解耦。

  • For a GET request received at some url, how can we display user-search-form, new-post-form and friends-list together?
    If we do that by some project-level common view, what about the app-level views?
    I have read that the {% include %} tag is handy for building template from several "subtemplates", but what about the different variables the included templates take?
  • How can we handle user-search-related GET requests, and new-post-related POST requests that may be sent from the same url?
    (The expression "requests sent from urls" may be inappropriate, but the user may send the different requests by acting on the page displayed at one specific url.)
  • Also, the different components should be kept decoupled.

注意:这个问题对于SO可能太笼统了,所以不做详细介绍答案我也很感谢消息来源,可能还会举例说明在行动中一起使用不同的应用程序。

推荐答案


对于在某个URL上收到的GET请求,我们如何显示
用户搜索表单,新帖子表单和好友列表?

For a GET request received at some url, how can we display user-search-form, new-post-form and friends-list together?

您可以请求从任何视图渲染任何模板。 Django将通过每个已注册的应用程序搜索模板,以找到 template 目录。或者,您可以使用 TEMPLATE_DIR 指定自己的模板目录,但是似乎您已经从下一个问题中知道了这一点...。

You can request to render any template from any view. Django will search for the template through every registered app for a template directory. Or you can specify your own template directories using TEMPLATE_DIR, but it seems like you already knew this from your next question....


如果我们通过某些项目级别的通用视图执行此操作,那么
应用程序级别的视图又如何呢?

If we do that by some project-level common view, what about the app-level views?

请问您可以尝试更深入地解释这个问题吗?

Could you please try to explain this question in more depth?


我读过{包括%}标签可方便地从多个子模板构建模板
,但是包含的模板采用的不同变量
呢?

I have read that the {% include %} tag is handy for building template from several "subtemplates", but what about the different variables the included templates take?

来自文档


包含的模板是使用包含模板的上下文
呈现的。

An included template is rendered with the context of the template that’s including it.

如果您有部分模板,则打算包括以下内容:

If you have a partial template, intended for including, like:

# hello.html
<h2>Hello {{ person.name }}</h2>

,另一个模板包括其中:

and another template is including it:

# greeting.html
{% include 'hello.html' %}


render_to_response('greeting.html', {})

输出为 Hello ,因为您在模板上下文中没有个人

the output will be Hello because you do not have a person in the template context

我不太明白您的第二点。您的django软件包只是一个python软件包。如果您有两个应用程序,则可以选择任何方式导入代码,从两个不同的应用程序导入模块。

I don't quite understand your second bullet point. Your django package is just a python package. If you had two apps you can import code any way you choose, importing modules from your two different apps.

例如,在大多数项目中,我最终得到的< c $ c>常用应用。它通常包含整个django项目中使用的实用程序。它通常包含我的基础测试课程。

For example, in most projects I end up having a common app. This usually contains utilities used throughout the whole django project. It usually contains my base test classes.

在您的应用中,您只需导入

In your apps you can just import

从yourproject.common.utils导入a_function

为帮助解耦django提供了信号框架。您的应用可以发出和接收信号。

To help with decoupling django provides a signals framework. Your apps can emit and receive signals.


Django包含一个信号分配器,该信号分配器有助于在操作时通知解耦的
应用程序出现在
框架中的其他位置。简而言之,信号使某些发送者可以通知某组接收者已经采取了某些措施。当许多代码片段可能对相同事件感兴趣时,它们特别有用。$ p

Django includes a "signal dispatcher" which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.

这篇关于Django-在项目中一起使用不同的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆