【问题标题】:Page is not showing on ID页面未显示在 ID 上
【发布时间】:2020-03-26 18:29:39
【问题描述】:

在 Django 中,当我单击返回 id 但页面未显示返回的链接时。

这是网址,

url(r'^(?P<pk>[-\w]+)/$', views.SchoolDetailView.as_view(),name = 'detail')

查看.py

class SchoolDetailView(DetailView):
context_object_name = 'school_detail'
model = models.School
template_name = 'basic_app/school_detail.html'

HTML 视图

 {% for school in schools %}
  <h2><li><a href="{{school.id}}">{{school.name}}</a></li></h2>
  {% endfor %}

【问题讨论】:

    标签: django


    【解决方案1】:

    你需要调用detail视图如下:

    {% for school in schools %}
        <h2><li><a href="{% url "detail" school.id %}">{{school.name}}</a></li></h2>
    {% endfor %}
    

    【讨论】:

      【解决方案2】:

      使用{% url %} 模板标签在您的模板中提供网址

      <a href="{% url 'detail' school.id %}">{{school.name}}</a>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 2012-09-29
        • 2016-04-13
        • 2014-04-14
        • 2018-12-25
        相关资源
        最近更新 更多