在 Django 中创建模板子目录的视图

发布于 2025-01-05 09:43:39 字数 618 浏览 0 评论 0原文

我想创建一个 TemplateView 来显示特定目录下的所有模板。

例如,我有

/staticpages/about-me.html
/staticpages/about-you.html
/staticpages/about-us.html

...

(更多)

在我的 urls.py 中我有 ..

url(r'^(?P<page_name>[-\w]+)/$', StaticPageView.as_view()),

..

在我的views.py 中,

class StaticPageView(TemplateView):
    def get_template_names(self):
        return 'staticpages/%s' % self.kwargs['page_name']

但是如果有人访问url /staticpages/blahblah.html (不存在),它会被该视图接受并生成模板未找到错误。如果找不到模板,我可以重定向到 404 吗?

或者有更好的方法来做到这一点吗?

I'd like to create a TemplateView that displays all templates under a specific directory.

So for example I have

/staticpages/about-me.html
/staticpages/about-you.html
/staticpages/about-us.html

...

(many more)

In my urls.py i have
..

url(r'^(?P<page_name>[-\w]+)/

..

In my views.py i have

class StaticPageView(TemplateView):
    def get_template_names(self):
        return 'staticpages/%s' % self.kwargs['page_name']

However if someone goes to the url /staticpages/blahblah.html (which doesn't exist), It gets accepted by this view and a template not found error is generated. Howe can I redirect to a 404 if template not found?

Or alternately is there a better way of doing this?

, StaticPageView.as_view()),

..

In my views.py i have

However if someone goes to the url /staticpages/blahblah.html (which doesn't exist), It gets accepted by this view and a template not found error is generated. Howe can I redirect to a 404 if template not found?

Or alternately is there a better way of doing this?

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

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

发布评论

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

评论(1

枕头说它不想醒 2025-01-12 09:43:39

您可以考虑使用项目设置,它将为您提供模板目录。然后,您可以使用 os.listdir ( http://docs.python.org/ library/os.html#os.listdir )列出该目录中存在的所有模板。以下是实现这一目标的方法。 (以下代码未经测试..只是为了给您一个想法)

模板列表可以这样显示:

# views.py
import os
from django.conf import settings

template_directory = os.path.join(settings.TEMPLATE_DIRS,'sub_directory')
templates = os.listdir(template_directory)
return render_to_response('template_list.html')

相应的模板文件..

# template_list.html
<ul>
{% for template in templates %}
  <li> <a href="/{{template}}"> {{template.filename}} </a> </li>
{% endfor %}
</ul>

希望有帮助..

You can consider using the project settings which will give you the templates directory. You can then use the os.listdir ( http://docs.python.org/library/os.html#os.listdir ) to list all the templates present in that directory. Here is how one can achieve it. (The following code is not tested.. it is just to give you an idea)

The list of templates can be displayed like this:

# views.py
import os
from django.conf import settings

template_directory = os.path.join(settings.TEMPLATE_DIRS,'sub_directory')
templates = os.listdir(template_directory)
return render_to_response('template_list.html')

The corresponding template file..

# template_list.html
<ul>
{% for template in templates %}
  <li> <a href="/{{template}}"> {{template.filename}} </a> </li>
{% endfor %}
</ul>

Hope that helps..

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