【问题标题】:Class views in Django, Html not showingDjango中的类视图,Html不显示
【发布时间】:2020-02-04 01:00:40
【问题描述】:

我对为什么我的 HTML 模板没有显示感到困惑。

我正在尝试学习类基础视图而不是 Django 上的函数。

我知道 URL 可以正常工作,因为 {% extend base.html %} 正在显示,但是像 h1 标记之类的其他任何内容都没有显示在渲染中?

谁能帮帮我。

views.py

from django.shortcuts import render

from django.views import View
from django.views.generic import (
    CreateView,
    DetailView,
    ListView,
    UpdateView,
    ListView,
    DeleteView
)

from .models import Article

class ArticleListView(ListView):

    template_name = 'article/article_list.html'
    queryset = Article.objects.all()

url.py

from django.contrib import admin
from django.urls import path

from .views import (
    ArticleListView
)
app_name = 'article'
urlpatterns = [
    path('', ArticleListView.as_view(), name = 'article_list'),

article_list.html

{%extends 'base.html'%}


<h1> Test </h1>
<h1> Test </h1>
{%block content%}
{% for instance in object_list %}

    <p>{{instance.id}} - <a href = '{{instance.get_absolute_url}}'> {{instance.title}} </a></p>

{%endfor%}
{%endblock%}

[This is the outcome when i request get html, The current html is coming from base.html][1]


  [1]: https://i.stack.imgur.com/W09EE.png


【问题讨论】:

  • 您必须将这些 h 标签放入您的块中才能渲染和显示它们
  • 嗨 beer44,我试过了。除了 base.html 中的 html 之外,它仍然没有渲染任何内容
  • 显示你的 base.html 模板。
  • 我认为 Django 有问题。从那以后,我删除了所有表并重置了所有内容并进行了迁移。现在它似乎又开始工作了。 >.

标签: python html django


【解决方案1】:

在视图中使用此代码:

context_object_name = 'article'
model = models. Article

在模板中使用这个:

article. Id 
{{article.get_absolute_url}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2015-04-15
    • 2013-08-11
    • 2019-11-10
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多