【问题标题】:Django: can't access to a folder of template of specific applicationDjango:无法访问特定应用程序的模板文件夹
【发布时间】:2018-05-26 20:45:10
【问题描述】:

我是 Django 的新手,我创建了一个项目“第一个 pycharm”,其结构如下:

    \firstpycharm
|   manage.py
|   views.py
+---firstpycharm
|   |   middleware.py
|   |   models.py
|   |   settings.py
|   |   urls.py
|   |   wsgi.py
|   |   __init__.py
|   |   
|   +---__pycache__
+---app
|   |   models.py
|   |   models.pyc
|   |   urls.py
|   |   views.py
|   +---migrations
|   +---static
|   |   +---build
|   |   +---images
|   |   +---vendors
|   +---templates
|   |   **\---app
|   |       |   base_site.html
|   |       |   base_site_bk.html
|   |       |   index.html
|   |       |   index2.html
|   |       |   index3.html
|   |       |   index_bk.html
|   |       |   invoice.html
|   |       |   level2.html
|   |       |   login.html
|   |       |   map.html
|   |       |   media_gallery.html
|   |       |   morisjs.html
|   |       |   other_charts.html
|   |       |   page_403.html
|   |       |   page_404.html
|   |       |   page_500.html
|   |       |   plain_page.html
|   |       |   pricing_tables.html
|   |       |   profile.html
|   |       |   projects.html
|   |       |   project_detail.html
|   |       |   sidebar.html
|   |       |   tables.html
|   |       |   tables_dynamic.html
|   |       |   top_navigation.html
|   |       |   typography.html
|   |       |   widgets.html
|   |       |   xx.html
|   |       \---report
|   |               audiance_overview.html**
+---static
|   \---script
+---staticfiles
|   \---admin
|       +---css
|       +---fonts
|       +---img
|       |   \---gis
|       \---js
|           +---admin
|           \---vendor
|               +---jquery
|               \---xregexp
+---templates

我的第一个 pycharm 使用一个名为“app”的应用程序,settins.py 有内容:

..........

INSTALLED_APPS = [
    'django_cassandra_engine',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',
    'app',
    'firstpycharm',
]

..........
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_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.contrib.messages.context_processors.messages',



            ],
        },
    },
]

我的问题是我可以访问 app/templates/app/ 中的所有 html 文件。但是当我在 app/templates/app/report 中创建 1 个文件夹作为报告时,我无法访问 report/audiance_overview.html。

请帮我访问它们。

谢谢, 詹姆斯

【问题讨论】:

  • “无法访问”是什么意思。您尝试以哪种方式访问​​文件?在模板中?在 url.py 中?在视图中?
  • 我无法通过 localhost:8000/index.html 访问但无法访问 localhost:8000/report/audiance_overview.html
  • 你能在这里复制粘贴urls.py吗?
  • 我是app/report/audiance_overview.html
  • @Ykh 请编辑问题并在此处发布urls.py 的内容。谢谢。

标签: python django django-templates django-apps


【解决方案1】:

您似乎混淆了文件路径和 url,这在 Django 中是两个不同的东西(例如,如果您来自 php 世界,那么它的行为就不一样了)。 如果您正在运行 django 开发服务器,您将无法以这种方式访问​​模板。

简而言之,您需要在urls.py 中定义一个指向视图的url,例如在views.py 中,这反过来又会调用模板。有关其工作原理的详细示例,请参阅 https://docs.djangoproject.com/en/1.11/intro/tutorial03/

一开始可能看起来很重,但这让您可以更好地控制您的 url 方案,因为它不受任何文件系统限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-08
    • 2021-01-25
    • 1970-01-01
    • 2012-05-10
    • 2022-07-29
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多