Re: Generic view template seems to have empty input

21 views
Skip to first unread message

山村維宏

unread,
Nov 25, 2019, 6:48:13 AM11/25/19
to django...@googlegroups.com
Maybe you should use ListView, not DetailView.

2019年11月25日(月) 19:44 Manos Zeakis <eze...@gmail.com>:
Hi

I have declared a model and the simple views I created are working.

Now I am trying to move to generic views.

So I added in urls.py

    path('book2/<int:pk>/', views.DetailView.as_view(), name='detail'),

And in views.py

class DetailView(generic.DetailView):
    model = Book

And the template is

{% block content %}

  <h1>book List</h1>
  {% if object_list %}
  <ul>
    {% for book in object_list %}
      <li>
        {{ book.author }}
      </li>
    {% endfor %}
  </ul>
  {% else %}
    <p>There are no books in the library.</p>
  {% endif %}      

{% endblock %}

For reference the models.py had been

class Book(models.Model):
    author = models.CharField(max_length=70)

But the result is "There are no books in the library". So it seems that the books list is not transferred to the template to be rendered. Could you help me understand where I have done wrong?

Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0c7de55c-4923-447a-9bf0-0ea962ca3542%40googlegroups.com.

山村維宏

unread,
Nov 25, 2019, 6:49:02 AM11/25/19
to django...@googlegroups.com
Sorry, I mistaked.
You can use Detail view.

DetailView pass your model data, which named "book"(because model name is Book).
Model doesn't list.
Reply all
Reply to author
Forward
0 new messages