簡體   English   中英

Cookiecutter Django - ListView 需要與視圖不同目錄中的模板?

[英]Cookiecutter Django - ListView expects templates in different directory to Views?

Cookiecutter 在my_project目錄下自動生成應用users 我已將my_app放在同一目錄中/將其列在INSTALLED_APPS下,它工作正常。 然后我在my_project/templates/my_app/下創建了我的模板文件夾(與生成的users模板相同)。

使用視圖時,我收到了TemplateDoesNotExistError 所以相反,我將模板放在my_project/my_app/templates下,在這種情況下它可以工作。 當我使用 ListView 時,我收到了同樣的錯誤。 所以我把這個模板移到了上一個文件夾中,它可以工作了。

- requirements
- my_project
    - contrib
    ▒   - sites
    ▒       - migrations
    - static
    ▒   - css
    ▒   - fonts
    ▒   - images
    ▒   ▒   - favicons
    ▒   - js
    - templates
    ▒   - account
    ▒   - pages
    ▒   - my_app
    ▒           - item_list.html <- only works here (ListView)
    ▒   - users
    - my_app
    ▒   - migrations
    ▒   - templates
    ▒           - item_detail.html <- only works here (View)
    - users
    ▒   - migrations
    ▒   - tests
    - utils

我在這里想念什么? 顯然,我想將我的模板放在一個文件夾中。 如果我切換模板/目錄,我會得到TemplateDoesNotExistError

這些是我的相關(我認為?)設置。

settings/base.py

ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
APPS_DIR = ROOT_DIR / "my_project"

INSTALLED_APPS = [
    ...
    "my_project.users",
    "my_project.my_app",
    ...
]

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [str(APPS_DIR / "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.i18n",
                "django.template.context_processors.media",
                "django.template.context_processors.static",
                "django.template.context_processors.tz",
                "django.contrib.messages.context_processors.messages",
                "my_project.users.context_processors.allauth_settings",
            ],
        },
    }
]

my_project/my_app/apps.py

from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _


class MyAppConfig(AppConfig):
    # default_auto_field = "django.db.models.BigAutoField"
    name = "my_project.my_app"
    verbose_name = _("My_App") # not really this but you get it

    def ready(self):
        try:
            import my_project.my_app.signals  # noqa F401
        except ImportError:
            pass

根據這個答案,也許我配置錯誤INSTALLED_APP

  1. 在 config/settings/base.py 的 LOCAL_APPS 上添加<project_slug>.<name-of-the-app>.apps.<NameOfTheAppConfigClass>

編輯: my_project.my_app.apps.MyAppConfig添加到 LOCAL_APPS 並沒有什么區別......

12 小時后,我按照編寫良好的指南重新完成了整個 cookiecutter django 流程,然后復制了我的模型、視圖、url、模板 - 問題仍然存在......

值得慶幸的是,該指南顯示了許多視圖示例(與文檔不同:),我注意到了我的觀點:

class ItemView(LoginRequiredMixin, View):
    """ Item Detail """

    template_name = "item.html"

本來應該:

class ItemView(LoginRequiredMixin, View):
    """ Item Detail """

    template_name = "my_app/item.html"

現在一切都在同一個模板文件夾下愉快地生活。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM