最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Is it possible to implement a DetailView of an object model in django-mptt-url categories using the absolute path of th

matteradmin7PV0评论

I want to write an online store using django and javascript, where javascript is only for decoration. You need to implement categories and several subcategories in subcategories. In theory, "django-mptt" is well suited for this (.html ). We build a tree of categories and subcategories, display a link to the product slug, and everything works well. But only the last category and the product name are displayed in the address bar, and I want the full path. I take "django-mptt-urls" as a basis ( ) there, "Photo" is used as the final object. We are building a path based on the Category model, and a list of end objects in "templates/gallery/category" {% for photo in instance.photos.all%} is available in the subcategory. There is a problem when you try to display the Photo model on a separate page in a separate DetailView:

  1. I create absolute_path using instance.get_absolute_url and photo.slug to the desired page, we don't have absolute_path in the Photo table. Is it even possible to display the desired page in this way?
  2. Having received absolute_path in the url, django first reads it as a category r'^gallery/(?P.*)$'. How to differentiate the paths?

we get the absolute path in template:

{% for photo in instance.photos.all %}
<li>{{ photo.name }}
{% with final_path=instance.get_absolute_url|add:'/'|default:'' %}
{% if final_path %}
{% with final_path|slice:"1:" as sliced_path %}
<a href="{% url 'object' sliced_path|add:photo.slug %}">{{ photo.name }}</a>
{% endwith %}
{% else %}
{{ photo.name }}
{% endif %}
{% endwith %}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far