简体   繁体   中英

Django HTML-how to make the title of an object 'clickable' and direct to detail display page after clicking

Sorry if this question is a bit general, I am learning web development in django and trying to figure out how the html functions & interacts.

Now,there is a simple page display.html,taking a set of objects(eg book) and display the title of the book.

Normally,the title of each book would be a link , clicking on the title link will direct to another page displaying the detailed info of this book(eg title, author)

But after searching through for long, I am still confused on how this could be done.

Below is the code for display.html.

<table>
{% for instance in book %}
<li>{{ instance.title }}</li>
{% endfor %}   
</table>

I guess there should be a function in the view.py accepting the id of a certain book and when you click on the tile of book,the id is passed to this function, and the function will pass the exact book object to a detail_display.html

Please correct me I am wrong and give some suggestion on how this could be done. Thanks very much!

The usual way I approach this in Django is to implement the get_absolute_url() function for the particular model class, ie Book in your case.

Resulting template code would be:

{% for b in book %}
<li>
    <a href="{{ b.get_absolute_url }}">{{ b.title }}</a>
</li>
{% endfor %}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM