【问题标题】:Url doesnt run the proper view in pinax project blogUrl 在 pinax 项目博客中没有运行正确的视图
【发布时间】:2020-11-16 03:59:59
【问题描述】:

最后提到的问题。

所以我安装了 pinax 博客应用程序并添加到我安装的应用程序中,迁移并设置了它的所有依赖项。 然后我将它添加到我项目中的 urlpatterns 中:

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r"^blog/", include("pinax.blog.urls", namespace="pinax_blog")),
    url(r"^ajax/images/", include("pinax.images.urls", namespace="pinax_images")),
]

pinax.blog.urls 中的 urpatterns 是:

urlpatterns = [
    url(r"^$", BlogIndexView.as_view(), name="blog"),
    url(r"^section/(?P<section>[-\w]+)/$", SectionIndexView.as_view(), name="blog_section"),
    url(r"^post/(?P<post_pk>\d+)/$", StaffPostDetailView.as_view(), name="blog_post_pk"),
    url(r"^post/(?P<post_secret_key>\w+)/$", SecretKeyPostDetailView.as_view(), name="blog_post_secret"),
    url(r"^feed/(?P<section>[-\w]+)/(?P<feed_type>[-\w]+)/$", blog_feed, name="blog_feed"),

    # authoring
    url(r"^manage/posts/$", ManagePostList.as_view(), name="manage_post_list"),
    url(r"^manage/posts/create/$", ManageCreatePost.as_view(), name="manage_post_create"),
    url(r"^manage/posts/(?P<post_pk>\d+)/update/$", ManageUpdatePost.as_view(), name="manage_post_update"),
    url(r"^manage/posts/(?P<post_pk>\d+)/delete/$", ManageDeletePost.as_view(), name="manage_post_delete"),

    url(r"^ajax/markdown/preview/$", ajax_preview, name="ajax_preview")
]

BlogIndexView:

class BlogIndexView(ListView):
    model = Post
    template_name = "pinax/blog/blog_list.html"
    search_parameter = "q"
    paginate_by = settings.PINAX_BLOG_PAGINATE_BY

    def get_current_section(self):
        return "all"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({
            "current_section": self.get_current_section(),
            "search_term": self.search_term()
        })
        return context

    def search_term(self):
        return self.request.GET.get(self.search_parameter)

    def search(self, posts):
        q = self.search_term()
        if q:
            posts = posts.filter(
                Q(title__icontains=q) | Q(teaser_html__icontains=q) | Q(content_html__icontains=q)
            )
        return posts

    def get_queryset(self):
        blog = hookset.get_blog(**self.kwargs)
        qs = Post.objects.current().filter(blog=blog).select_related("section", "blog")
        return self.search(qs)

问题:

当我运行服务器并转到http://127.0.0.1:8000/blog/ 时,BlogIndexView 应该根据 pinax_blog 的 url 模式的第一个 url 模式工作,并且应该加载 template_name = "pinax/blog/blog_list.html"。但模板不会加载名为 site_base.html 的不同模板,而是加载。任何想法为什么?

【问题讨论】:

    标签: python django blogs pinax


    【解决方案1】:

    blog_list.html 扩展 blog_base.html 进而扩展 site_base.html

    如果您向下跟踪site_base.html(可能在您的虚拟环境中),您会发现它没有提供由扩展它的模板填写的块。相反,它包含注释:

    {# This template intentionally left blank to satisfy test suites. Your project should always provide a site_base.html itself. #}
    

    因此,您要在项目中提供site_base.html,这将覆盖 pinax 提供的存根。

    对于初学者,您的site_base.html 可以提供正文块:

    {% block body %}{% endblock %}
    

    将其保存在&lt;your-app&gt;/templates/site_base.html 中,并确保在pinax.blog 之前将您的应用添加到INSTALLED_APPS(在您项目的settings.py 中),以便模板加载器在它们要在@ 中覆盖的模板之前发现它的模板987654333@.

    【讨论】:

    • 老兄,但是 blogindexview 有 template_name=...blog_list.html 。所以这不意味着 blog_index.html 应该被渲染。是的 blog_index.html 确实扩展了 site_base.html 但它有很多额外的东西。我什至尝试在其中放置一个带有文本的 ptag,以检查是否呈现 blog_index.html。但事实证明 site_base.html 正在呈现,即使 template_name=...blog_list.html 在视图中。
    • 它是最终呈现的指定模板的基本模板。如果指定的模板是它自己的基本模板,那么它会被渲染。在您的情况下,blog_list.htmlsite_base.html 为基础。所以,最终,site_base.html 被渲染(它的块被替换为扩展它的模板中的匹配块)。
    【解决方案2】:

    模板不会加载名为 site_base.html 的不同模板,而是加载。任何想法为什么?

    我想这里的问题是回答为什么模板加载 site_base.html 并且不显示任何内容。现在上面的答案是正确的,但让我帮助解决问题的人的挫败感,以获得更多的清晰度。

    这是因为 site_base.html 没有任何可以扩展的块,因此加载时没有任何显示。

    {# This template intentionally left blank to satisfy test suites. Your project should always provide a site_base.html itself. #}
    

    所以只需将这段代码添加到 site_base.html

    {% block body %}{% endblock %}
    

    会给你一些可见的东西,这就是你需要做的。

    【讨论】:

      最近更新 更多