【问题标题】:How do I use a template from one app in another app如何在另一个应用程序中使用来自一个应用程序的模板
【发布时间】:2019-07-10 20:40:18
【问题描述】:

目前正在尝试制作社交应用程序。我创建了两个应用程序 Main 和 posts。我的帖子应用程序将处理创建、删除帖子、帖子列表的所有功能。我的主应用程序是我的主页,它将显示所有帖子,例如类似于 facebook 的主页。

主应用程序当前对用户进行身份验证,以及什么不是。我为我的帖子应用程序创建了视图和模板,现在我想将它包含在 Main 的应用程序主页上。不知道如何出去做这件事。不要求任何人编写代码,但至少给出如何实现这一点的结构。

应用的基本结构:

--Main_app
  --_views.py
  --templates
    --home.html
--posts_app
  --_views.py
  --templates
    --posts.html

我的 posts_app 视图当前包含的文件之一是:

def posts_list(request):
    #return HttpResponse("<h1> List a posts. </h1>")
    render(requests, "posts.html", {})

templates/posts.html 包含这个文件:

<!DOCTYPE html>
<html lang="en">
  <body>
    <h1>Posts template is working</h1>
  </body>
</html>

在我的 main_app 模板/html 中:

{% if user.is_authenticated %}
<h1>Homepage</h1>
<!-- Posts template -->
{% include "posts/templates/posts.html" %} 
{% endif %}

当时对我来说,将模板从帖子应用程序导入到主应用程序是有意义的,它会起作用。显然没有工作,所以我做了一些研究,找不到明确的解决方案,是否有称为模板继承的东西,应该将我的视图从帖子导入到 main.js 中。我不知道。任何帮助表示赞赏。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    如果您想使用相同的模板,最好的方法是在项目目录中而不是在应用程序中添加模板。

    -myproject\
              -Main_app\
              -posts_app\
              -templates\
                         -template_name.html
    

    并在设置中添加,

        TEMPLATES = [
           {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],# Add this line
            'APP_DIRS': True, 
             ........
    

    在您看来,您可以直接参考template_name.html

    另一种方法是在目录中创建模板,如

    myproject\
             -Main_app\
                       -templates\
                                 -Main_app\
                                            mytemplate.html
             -posts_app\
    

    现在你可以通过引用Main_app\mytemplate.html来使用它

    在这种情况下,第一个非常有效。

    【讨论】:

    • 好的,关于第一种方式,我将把 main.html 和 posts.html 放在根目录的模板文件夹中。然后我会将我的posts.html 导入main.html,对吗?我还会在 posts.views 中渲染 posts.html 吗?
    • 我是否包含* {% include "posts/posts.html" %} --> 在 shared.html 中
    【解决方案2】:

    您可以将模板文件夹放在项目目录的根目录中。所以不要有

    --Main_app
      --_views.py
      --templates
        --home.html
    --posts_app
      --_views.py
      --templates
        --posts.html
    

    你可以的

    --Main_app
      --_views.py
    
    --posts_app
      --_views.py
    --templates
      --SHARED.html
      --main
        --home.html
      --posts
        --posts.html
    

    在您看来,您可以执行以下操作:

    return render(request, 'SHARED.html', context)  # For shared
    
    return render(request, 'posts/posts.html', context)  # For template in posts
    

    【讨论】:

    • 我必须在 shared.html 中执行 {% include "posts/posts.html" %} --> 吗?
    【解决方案3】:

    最好在您的根项目文件夹中创建一个单独的templates 目录。这样您就可以在任何您希望的应用程序中访问 html 文件。您的目录结构如下所示:

    .
    ├── blog
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   │       ├── 0001_initial.cpython-35.pyc
    │   │       └── __init__.cpython-35.pyc
    │   ├── models.py
    │   ├── __pycache__
    │   │   ├── admin.cpython-35.pyc
    │   │   ├── apps.cpython-35.pyc
    │   │   ├── __init__.cpython-35.pyc
    │   │   ├── models.cpython-35.pyc
    │   │   ├── urls.cpython-35.pyc
    │   │   └── views.cpython-35.pyc
    │   ├── tests.py
    │   ├── urls.py
    │   └── views.py
    ├── blog_project
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-35.pyc
    │   │   ├── settings.cpython-35.pyc
    │   │   ├── urls.cpython-35.pyc
    │   │   └── wsgi.cpython-35.pyc
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── db.sqlite3
    ├── manage.py
    ├── Pipfile
    ├── Pipfile.lock
    ├── static
    │   └── css
    │       └── base.css
    └── templates
        ├── base.html
        ├── home.html
        ├── post_detail.html
        └── post_new.html
    

    This is a simple blog I made using Django

    如果您需要参考,这可能会有所帮助。

    【讨论】:

    • 好吧有道理,但是如何让 post_new.html 显示在 base.html 或 home.html 中?我只需要在 base.html 或 home.html 中使用 {% include "posts_new.html" %} 吗?
    • @alongtest12 你可以extend 模板,不包括它们。也就是说,如果您的逻辑是这样的,您可能会在home.html 中包含post_new.html。你可以google一下扩展模板或者查看我分享的项目链接中的html文件。
    【解决方案4】:

    至于通过不同的应用程序交叉加载模板,我建议将您的项目结构化为templates 是一个单独的目录,这将在两个方面使您受益:

    1. 以后将前端与后端完全分离会更容易。
    2. 它将增加模块化,因为它会减少您的应用程序之间的耦合。

    示例模板总监:

    ── templates
    │   ├── main
    │   ├── another_cool_app
    │   ├── accounts
    │   ├── posts
    │   │   ├── timeline.html
    │   │   ├── post_details.html
    

    等等。

    要在其他模板中加载模板,您可以简单地:

    {% include "posts/timeline.html" with posts_list=user.friends_posts %}
    

    编码愉快。

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 1970-01-01
      • 2019-12-29
      • 2011-06-02
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2010-11-10
      相关资源
      最近更新 更多