django 如何在另一个应用中使用一个应用中的模板

lsmd5eda  于 2023-03-24  发布在  Go
关注(0)|答案(4)|浏览(185)

目前正在尝试使一个社会应用程序。我创建了两个应用程序主要和职位。我的职位应用程序将处理所有功能的创建,删除一个职位,职位列表。我的主要应用程序是我的主页,将显示所有职位,例如类似于Facebook的主页。
主应用程序目前验证用户和什么不.我创建了我的帖子应用程序的视图和模板,我现在想把它包括在主的应用程序主页.不知道如何去做这件事.不要求任何人写了代码,但至少给予了一个如何实现这一点的结构.
App的基本结构:

--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 templates/HTML中:

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

这是有意义的,我当时只是从帖子应用程序导入模板到主应用程序,它会工作.没有明显的工作,所以我做了一点研究,找不到一个明确的解决方案,是不是有所谓的模板继承,应该导入我的意见从帖子到主.我不确定.

wwodge7n

wwodge7n1#

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

-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来使用它
第一个在这种情况下非常有效。

lymnna71

lymnna712#

你可以做的是把你的模板文件夹放在你的项目目录的根目录下。

--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
9vw9lbht

9vw9lbht3#

最好在你的根项目文件夹中创建一个单独的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
如果你需要推荐信的话可能会有帮助。

x6h2sr28

x6h2sr284#

至于通过不同的应用程序交叉加载模板,我建议将您的项目结构化,使templates成为一个单独的目录,这将在两个方面为您带来好处:
1.以后将前端与后端完全分离会更容易。
1.它将增加模块性,因为它将减少应用程序之间的耦合。
模板控制器示例:

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

等等。
要在其他模板中加载模板,您只需:

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

编程愉快。

相关问题