如何绑定多个可重用的Django应用程序在一起? [英] How to bind multiple reusable Django apps together?

查看:76
本文介绍了如何绑定多个可重用的Django应用程序在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽力编写可重用的Django应用程序。现在我很困惑如何把它们放在一起来得到最终的项目。



这是一个我的意思的例子:
我有一个图片应用程序存储,调整大小并显示图像。还有一个weblog应用程序存储,编辑和显示文本。现在我想结合这两个来显示博客文章的图片。



为了做到这一点,我可以把外部关键字段放在博客中来指向图片。但是,如果没有图片应用程序,则无法使用该博客。另外我可以创建一个第三个应用程序,它负责连接两者。



这是最好的做法是什么?



编辑:感谢您的非常好的答案,但我仍然在寻找如何解决这个问题的更实际的例子。要完成我的例子:有时使用没有图片应用程序的博客应用程序会很好。但是如果我硬编码依赖,那就不再可能了。那么第三个应用程序如何组合?

解决方案

介绍在底部的答案(更直接的答案)。我会假设你有一个文本处理应用程序称为文本和一个应用程序的图片处理称为图片,第三个应用程序博客博客。



大图片



您将需要学习关于python程序员的模板语言的手册。这个想法是每个东西都在自己的应用程序中,并且你有一个连接所有内容的第三个应用程序。应用程序应该随时提供其模型和视图(只需记住让您专注于应用程序应该做什么),并提供一组模板标签。



如何制作包含标签



制作包含标签,真的很简单!它将提醒您编写正常视图。



在您的应用文件夹中创建一个目录模板标签。还要在此模板标签中创建一个 init .py文件(所以该目录变成一个python包)。



然后创建一个python文件。该名称很重要,您将在使用您的应用程序的模板中的 {%load xyz%} 中使用此名称。例如,如果调用文件 picturestags.py ,您将致电

{%load picturestags%} 在所有使用它的模板中。



首先在文件中添加一些政治,你不需要考虑太多,只是在其他任何事情之前加入:

  from django.template import Library 
register = Library()

然后通过定义与标签名称相同的函数来添加标签。我将在示例中将其称为display_picture,它将使用一个参数url。该函数应该创建一个您将在模板中使用的字典。我的示例将只显示网址指向的图片。

  @ register.inclusion_tag('pictures / display_picture.html') 
def display_picture(url):
return {'picture':url}

在应用程序中创建路径模板/图片,并创建文件display_picture.html,其中包含:

 < img src = {{picture}}/> 

你可能明白,@register使它成为一个标签,什么是字典中的display_picture返回你可以在display_picture.html中使用什么。非常喜欢你的普通视图功能。



最后你会得到这些文件:

 图片/ 
__init__.py
models.py
view.py
tests.py
templates /
图片/
display_picture.html
templatetags /
picturetags.py

是您需要添加到您的图片应用程序。要在您的博客应用程序中使用此功能,您需要将图片添加到您的INSTALLED_APPS中。然后在模板中,您需要使用自己的新家庭烘焙标签加载它: {%load picturestags%} 然后只需添加标签 {%display_picture https://www.google.com/intl/sv_ALL/images/logos/images_logo_lg.gif%} 如下所示:

  {%load picturestags%} 
< html>
< body>
{%display_picture https://www.google.com/intl/sv_ALL/images/logos/images_logo_lg.gif%}
< / body>
< / html>



结果



这只是一个小例子,但你可以看到这是非常容易扩展这个。您的博客可以通过导入他们的模型和外键来连接文本和图片应用程序。有一个博客文章连接文本和图片。您的blog_post.html模板可能看起来像(简化):

  {%load picturestags%} 
{%load texttags%}
< html>
< body>
< h1> {{subject}}< / h1>
< div class =article> {%article_to_html articleid%}< / div>
< div class =article_picture> {%display_picture%}< / div>
< / body>
< / html>请注意,只有博客具有依赖关系,它应该具有依赖性(没有文本和图片的博客) ...但图片可以没有文字生活)。外观和布局应由CSS和DIV / SPAN标签控制。以这种方式你可以把你的图片应用程序,并把它给一个谁不了解的文本应用程序和使用它,以不同的方式显示图片可能没有触摸你的代码!



包含标签是我昨天才知道的唯一知道的事情。我认为Django为方便生活而设的方便。在文档页面上有更多的内容(包括如何使真实标签的硬道路没有快捷方式)。所以如果你发现这个方法有限,阅读文档...它有很多例子。它还讨论了如何制作过滤器,simple_tags,线程注意事项和其他高级内容。



简介



这正是这个问题,因为你和我也想要的东西不同于我读的答案(我不说答案是坏的,他们帮助我学到很多,并给了我深刻的见解,但我想这个我现在在写)。我设法弄清楚一些不是很明显的东西,谢谢你的问题,绝对感谢Stack Overflow,所以这是我的贡献,甚至可能已经放弃了一个半年的问题(可能会帮助谷歌或者两个)! p>

我也从 Google Tech Talk Reusable Apps 。最后(43分钟)他提到了一些很好的例子,例如 django-tagging ,他是如何编写可重用的应用程序的模型。这给了我所有这一切的想法,因为这是django标签解决这个我们有/有的这个问题的方式。



现在我写完所有这一切小时),我觉得我第一次可以贡献而不是谷歌,并遵循别人在做什么或抱怨别人不写我需要做的事情。我第一次负责写我的观点,所以其他人可以google这个(只是不得不写这段话:-),因为它感觉真的很棒,即使它可能被撕碎或被忽略和遗忘)。


I try my best to write reusable Django apps. Now I'm puzzled how to put them all together to get the final project.

Here is an example of what I mean: I have a picture app that stores, resizes and displays images. Also I have a weblog app that stores, edits and displays texts. Now I want to combine these two to show blog posts with images.

To do that I could put foreign key fields in the blog to point at pictures. But then the blog could not be used without the picture app. Also I could create a third app, which is responsible to connect both.

What is the 'best practice' way of doing it ?

EDIT: Thank you for your very good answers, but I'm still looking for more practical example of how to solve this problem. To complete my example: Sometimes it would be nice to use the blog app without the picture app. But if I hard code the dependency it is no longer possible. So how about 3rd app to combine both ?

解决方案

Introduction talk in the bottom of the answer (more straight to the answer). I will assume that you have one app for text handling called Text and one app for picture handling called Pictures and a third app for blogging called Blog.

Big picture

You will need to study the manual about the template language for python programmers. The idea is that each thing is in its own app and that you have a third app that connects everything. The apps should then supply its models and views as you like them (just remember to keep you focus on what the app should do) and also supply a set of templatetags.

How to make inclusion tags

Make inclusion tags and it is really easy! It will remind you of writing normal views.

Create a directory templatetags in you app folder. Also create a init.py file in this templatetags (so the directory becomes a python package).

Then create a python file. The name is important, you will use this in {% load xyz %} in the templates that will use your app. For instance if call the file picturestags.py, you will call
{% load picturestags %} in all templates that will use it.

First in the file add some politics you need not to think much about, just include this before anything else:

from django.template import Library
register = Library()

Then add the tags by defining functions with the same name as your tag. I will call it display_picture in the example and it will take one argument url. The function should create a dictionary that you will use in a template. My example will just display the picture the url is pointing to.

@register.inclusion_tag('pictures/display_picture.html')
def display_picture(url):
    return {'picture': url}

Create the path templates/pictures in your app and create the file display_picture.html inside containing:

<img src="{{ picture }}" />

As you probably understand, the @register makes this a tag, what is inside the dictionary display_picture returns are what you can use in display_picture.html. Very much like your normal view functions.

In the end you will end up with these files:

pictures/
    __init__.py
    models.py
    views.py
    tests.py
    templates/
        pictures/
            display_picture.html
    templatetags/
        picturetags.py

That is all you need to add to your Picture app. To use this in your Blog app, you need to add Pictures to your INSTALLED_APPS. Then in the templates, where you need to use your own newly home baked tag first load it: {% load picturestags %} then just add the tag {% display_picture https://www.google.com/intl/sv_ALL/images/logos/images_logo_lg.gif %} like this:

{% load picturestags %}
<html>
    <body>
        {% display_picture https://www.google.com/intl/sv_ALL/images/logos/images_logo_lg.gif %}
    </body>
</html>

Results

This is just a small example but you can see that it is very easy to expand this. Your blog could connect the Text and Pictures app by importing their models and foreign key them. There is you connection Text and Pictures for a certain blog post. Your blog_post.html-template could look like (simplified):

{% load picturestags %}
{% load texttags %}
<html>
    <body>
        <h1>{{ subject }}</h1>
        <div class="article">{% article_to_html articleid %}</div>
        <div class="article_picture">{% display_picture %}</div>
    </body>
</html>

Notice that only the Blog has dependencies and it is dependencies it should have (no blog without text and pictures...but pictures can live without text). The look and placement should be and can be controlled by CSS and DIV/SPAN-tags. In this way you can take your Picture app and give it to someone who has no idea about Text app and use it, displaying pictures in different ways probably without ever touching your code!

Inclusion tags is the only thing I know of since I just learned this yesterday. I think it is a convenience provided by Django to make life simple. On the documentation page there are a whole lot more (including how to make "real" tags the hard way without "shortcuts"). So if you find this method to limited, read the documentation...it has lots of examples. It also discusses how to make filters, simple_tags, thread considerations and other advanced stuff.

Introduction

I had this exactly this problem as you and I also wanted something different than the answers I read (I don't say the answers was bad, they helped me learn a lot and gave me insights, but I wanted this I am writing now). I managed to figure something out that is not very obvious, thanks to your question and definitely thanks to Stack Overflow so this is my contribution back even to a half year old question that is probably abandoned (might help a googler or two)!

I also got a lot of inspiration from Google Tech Talk Reusable Apps. In the end (43 minutes) he mentions some good examples like django-tagging which is what he says a model for how to write reusable apps. That gave me the idea for all this, because that is the way django-tagging solves this very problem we had/have.

Now after I written all this (took an hour), I feel for the first time that I might contribute instead of just google and follow what others are doing or complaining that others are not writing how I need to do things. For the first time I am taking my responsibility of writing my view so others can google this (just had to write this paragraph :-) because it feels really great, even if it might be shredded to pieces or ignored and forgotten).

这篇关于如何绑定多个可重用的Django应用程序在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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