使用django-mptt-urls显示模型的详细信息页面

时间:2016-06-01 13:18:09

标签: python django django-mptt

我试图访问照片实例的详细信息页面,但我似乎无法让它工作。 (类别详细信息页面效果很好!)

我想访问http//domain.com/main-category/sub-cat/sub-sub-cat/photo-slug/

Models.py:

from mptt.models import MPTTModel, TreeForeignKey


class Category(MPTTModel):
    name = models.CharField('category name', max_length=32)
    parent = TreeForeignKey('self', null=True, blank=True, verbose_name='parent category', related_name='categories')
    slug = models.SlugField(unique=True)

    def get_absolute_url(self):
        return reverse('gallery', kwargs={'path': self.get_path()})


class Photo(models.Model):
    name = models.CharField('photo name', max_length=32)
    parent = TreeForeignKey(Category, verbose_name='parent category', related_name='photos')
    slug = models.SlugField()

    class Meta:
        unique_together = ('slug', 'parent')

urls.py:

url(r'^(?P<path>.*)', mptt_urls.view(model='gallery.models.Category', view='gallery.views.category', slug_field='slug'), name='gallery'),

views.py:

def category(request, path, instance):
    return render(
        request,
        'gallery/category.html',
        {
            'instance': instance,
            'children': instance.get_children() if instance else Category.objects.root_nodes(),
        }
    )

如何使用mtpp-urls访问照片模型?

编辑1:

很抱歉,当我尝试使用http//domain.com/main-category/sub-cat/sub-sub-cat/photo-slug这样的网址时,模板显示Gallery main page,因为当我尝试使用带照片的网址时没有instance(是的,照片将该类别作为父母:)

以下是category.html模板:

<html>
    <head>
        <title>{% if instance %}{{ instance.name }}{% else %}Gallery main page{% endif %}</title>
    </head>
    <body>
        <h3>{% if instance %}{{ instance.name }}{% else %}Gallery main page{% endif %}</h3>

        <a href="{% url 'gallery' path='' %}">Gallery main page</a>
        {% for ancestor in instance.get_ancestors %}
            > <a href="{{ ancestor.get_absolute_url }}">{{ ancestor.name }}</a>
        {% endfor %}
        > <strong>{{ instance.name }}</strong>

        <h4>Subcategories:</h4>
        <ul>
            {% for child in children %}
                <li><a href="{{ child.get_absolute_url }}">{{ child.name }}</a></li>
            {% empty %}
                <li>No items</li>
            {% endfor %}
        </ul>

        <h4>Photos:</h4>
        <ul>
            {% for object in instance.photos.all %}
                <li><a href="{{ object.slug }}">{{ object.name }}</a></li>
            {% empty %}
                <li>No items</li>
            {% endfor %}
        </ul>        

    </body>
</html>

1 个答案:

答案 0 :(得分:1)

它不起作用的原因是MPTT仅适用于一次一个模型,在这种情况下是画廊,而不是照片。

实际上,在您的模型中,图库是照片的父级,但只是照片上的外键。

如果你知道slug的最后一个元素总是一张照片,你可能会创建一个这样的中间视图:

urls.py:

url(r'^(?P<path>.*)', views.photo, name='photo'),

views.py:

def photo(self, path=''):
    path_parts = path.split('/')
    gallery_path = '/'.join(path_parts[:-1])
    photo_slug = path_parts[-1]
    return mptt_urls.view(model='gallery.models.Category', view='gallery.views.photo_in_category', slug_field='slug')({'path': gallery_path, 'photo_slug': photo_slug})

def photo_in_category(request, path, gallery_instance, photo_slug=None):
    photo_instance = Photo.objects.get(parent=gallery_instance, slug=photo_slug)
    ...

否则,您可能需要区分URLS(这也会带来更简单的代码):

urls.py:

url(r'^photo/(?P<path>.*)/(?P<photo_slug>[-\w]+)/$', mptt_urls.view(model='gallery.models.Category', view='gallery.views.photo_in_category', slug_field='slug'), name='photo'),
url(r'^gallery/(?P<path>.*)/$', mptt_urls.view(model='gallery.models.Category', view='gallery.views.category', slug_field='slug'), name='gallery'),