在django模板中将子项排序为父项

时间:2016-04-08 02:17:14

标签: django sorting filter

我在学习Django(Python)的过程中一直试图找出这个问题。

我想将子对象排序到父对象中。具体来说,我想将城市分为各自的州。然后,我希望城市以正确的状态显示在模板中。我想要的是这样的:

国家 ---城市 ---城市 ---城市

编辑:我有点工作了。但是,城市正在模板中重复。我只需要每一个显示一次。我已经包含了模板,看看是否有人可以帮助我。

再次感谢。

models.py

class State(models.Model):
    state_name = models.CharField(max_length=20, default='')
    state_slug = models.SlugField()
    state_image = models.ForeignKey(Image, null=True)
    state_summary = models.TextField(null=True)

    def __str__(self):
        return self.state_slug

class City(models.Model):
    city_name = models.CharField(max_length=55, default='')
    city_slug = models.SlugField()
    state_image = models.ForeignKey(Image, null=True)
    school_image = models.ForeignKey(Image, null=True, related_name='+')
    state = models.ForeignKey(State, null=True)
def __str__(self):
    return self.city_slug

views.py

class CityInStateView(ListView):
    model = City

    context_object_name = 'city_in_state_list'

    def get_context_data(self, **kwargs):
        context = super(CityInStateView, self).get_context_data(**kwargs)
        city = City.objects.all()
        state = State.objects.get(state_slug=self.kwargs['state_slug'])
        context['city_list'] = City.objects.filter(state=state).order_by('city_name')
        return context

urls.py

urlpatterns = [
    url(r'^$', SchoolIndexView.as_view(), name='school_index'),
    url(r'^(?P<state_slug>[\w-]+)/$', CityInStateView.as_view(), name='state_index'),
]

template.html

{% block main_content %}
<div class="row body">
    <div class="main_content">
        <div class="row">

            <div class="medium-12 columns small-centered">
                <div class="feature_wrapper">
                    {% load cloudinary %}
                    <header class="page_header">
                        <div class="row">
                            <div class="medium-12 columns">Top bar</div>
                        </div>
                    </header>
                    <div class="search">
                        <div class="row">
                            <div class="medium-12 columns">Search bar</div>
                        </div>
                    </div>
                    {% if city_in_state_list %}
                    {% for city in city_in_state_list %}
                    <section class="hero">
                        {% cloudinary city.state_image.image format="jpg" crop="fill" %}
                        <p class="photo-caption">
                            {{ city.state_image.image_name }} by {{ city.state_image.image_author }} via {{ city.state_image.image_source }} | {{ city.state_image.image_license }}
                        </p>
                    </section>
                    <section class="summary">
                        <p>{{ city.state.state_summary }}</p>
                    </section>
                    <div class="row">
                        <div class="medium-12 columns listicle">
                            <div class="demo_wrapper">
                                <div class="long_ad_box">
                                <img src="http://placehold.it/728x90">
                            </div>
                        </div>
                        <div class="state_name">
                            <h2 class="headline">Nursing Schools in {{ city.state.state_name }}</h2>
                        </div>
                        {% if city_list %}
                            {% for school in city_list %}
                            <h2 style="text-align: left";>{{ school.city_name }}</h2>
                            <div class="school_image">
                                {% cloudinary city.school_image.image format="jpg" crop="fill" %}
                            </div>
                            {% endfor %}
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    {% endif %}
</div>                                  
{% endblock %}

这已经让我的屁股踢了一个星期。请像我一样向我解释一下。我提前感谢您的所有帮助。

1 个答案:

答案 0 :(得分:1)

我认为你想使用一个表示状态的DetailView,而不是一个城市列表。这是因为您的URL代表州的对象视图。

所以,你可以

class CityInStateView(generic.DetailView):
    model = State
    template_name = 'template.html'
    slug_field = 'state_slug'

    def get_context_data(self, **kwargs):
        context = super(CityInStateView, self).get_context_data(**kwargs)
        context['state'] = self.object
        context['city_list'] = self.object.city_set.all().order_by('city_name')
        return context

然后你的模板(简化示例)就像:

一样简单
<h1>{{ state.state_name }}</h1>
{% for city in city_list %}
  <p>{{ city.city_name }}</p>
{% endfor %}

对于使用DetailView的URL,并且因为您没有使用slug作为字段名称,您需要告诉DetailView state_slug是您的slug。您可能希望将unique=True添加到模型中。

希望你能从这里拿走它