Django 模板上下文多个视图

发布于 2024-12-23 16:14:54 字数 430 浏览 4 评论 0原文

我正在运行 Idios,一个配置文件应用程序,我想将我的应用程序中的配置文件放在顶部idios(即保持 idios pip 可安装,而不修改应用程序本身)。

问题是,配置文件视图是在 idios 应用程序中编写的,使用传递上下文变量的面向对象方法,并在模板中显示该视图。我希望个人资料视图还包括用户朋友的列表,由单独的朋友应用程序管理。我仅通过将朋友导入 Idios 并使用新的friend_list 变量更新上下文,并添加另一个模板块来显示朋友列表,就获得了概念验证。

那么,在不改变 idios 应用程序的情况下组合好友列表和个人资料的最佳或最明智的方法是什么?这个问题基本上是请求帮助真正以支持可重用应用程序的 DRY 方式理解 Django 中的 MVT 系统。

I'm running Idios, a profile app, and I would like to make the profiles in my app on top of idios (i.e. keep idios pip-installable, without modifying the app itself).

The problem is, the profile view is written in the idios app, using the object-oriented approach of passing context variables, and displaying that view in a template. I want the profile view to also include a list of a user's friends, managed by a separate friends app. I got a proof-of-concept by merely importing friends into Idios and updating the context with a new friend_list variable, and adding another template block to display the friends list.

So, what is the best, or most sane approach to combine both a friends list and the profile without altering the idios app? This question is basically a plea for help to really understand the MVT system in Django in a DRY way that supports reusable apps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱武穆 2024-12-30 16:14:54

值得庆幸的是,上述应用程序使用基于类的视图,这就是使用基于类的视图所带来的好处。要修改上下文,您只需创建其个人资料视图的子类:

yourapp/views.py

from idios.views import ProfileDetailView

class MyProfileDetailView(ProfileDetailView):
    def get_context_data(self, **kwargs):
        context = super(MyProfileDetailView, self).get_context_data(**kwargs)

        # get the list of friends and store it in a new key in `context`

        return context

然后,只需在 urls.py 中重新定义 idios 默认使用的 urlpattern(需要在 idios urlpatterns 之前)包含)并将其指向您的子类

最后,通过在您的项目(或应用程序的)templates 目录。复制默认模板,随意修改。由于 Django 提供了所有可用的覆盖,您根本不必实际接触原始源代码。

Thankfully, the mentioned app uses class-based views, and this is the benefit you get from using class-based views. To modify the context, you merely create a subclass of their profile view:

yourapp/views.py

from idios.views import ProfileDetailView

class MyProfileDetailView(ProfileDetailView):
    def get_context_data(self, **kwargs):
        context = super(MyProfileDetailView, self).get_context_data(**kwargs)

        # get the list of friends and store it in a new key in `context`

        return context

Then, just redefine the urlpattern idios uses by default in your urls.py (needs to go before the idios urlpatterns are included) and point it to your subclass instead

Finally, to override the template idios uses by creating an idios/whatever_template.html file in your project's (or app's) templates directory. Copy the default template and modify at will. You don't have to actually touch the original source at all thanks to all the overrides Django makes available.

绿萝 2024-12-30 16:14:54

您应该能够通过从项目中的应用程序中的 idios 创建 ProfileDetailView 类的子类来实现此目的,并为该用户的朋友添加额外的上下文。像这样:

import idios.views

class ProfileDetailView(idios.views.ProfileDetailView):

    def get_context_data(self, **kwargs):
        context = super(ProfileDetailView, self).get_context_data(**kwargs)
        context['friends'] = get_friends(self.page_user)  # Or however it works
        return context

然后您可以使用这个子类作为项目中的视图。

You should be able to do this by creating a subclass of the ProfileDetailView class from idios in an app in your project, adding in the extra context for that user’s friends. Something like this:

import idios.views

class ProfileDetailView(idios.views.ProfileDetailView):

    def get_context_data(self, **kwargs):
        context = super(ProfileDetailView, self).get_context_data(**kwargs)
        context['friends'] = get_friends(self.page_user)  # Or however it works
        return context

You can then use this subclass as a view in your project.

旧竹 2024-12-30 16:14:54

如果您认为您可能还想在其他视图中包含用户的朋友,那么一种简单的方法是创建一个 自定义模板标签或过滤器。然后您只需覆盖模板,并在其中包含您的模板标签/过滤器。

如果您只想在此特定视图中显示用户的朋友,那么我将按照其他答案中的描述覆盖 get_context_data

If you think you might want to include a user's friends in other views as well, a DRY way would be to create a custom template tag or filter. Then you would just have to override the template, and include your template tag/filter there.

If you only want to display the user's friends in this particular view, then I would override get_context_data as described in the other answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文