django将模板包含到其他模板中

时间:2013-11-29 22:30:00

标签: django django-templates django-views

我无法在index.html中正确包含“mainslider.html” {%include“pages / mainslider.html”%}

我的索引页观看次数

from django.shortcuts import render_to_response
def index(request):
    return render_to_response('index.html')

我可以在http://127.0.0.1:8000/slider/看到我的滑块,但没有http://127.0.0.1:8000/

它是滑块代码: 的 models.py

from django.db import models
from ckeditor.fields import RichTextField
from easy_thumbnails.fields import ThumbnailerImageField

class Portfolio(models.Model):
    title = models.CharField(max_length = 200)
    description = models.TextField(max_length = 500)
    meta_desc = models.CharField(max_length=200)
    meta_key = models.CharField(max_length=200)
    device = RichTextField()
    image = ThumbnailerImageField( upload_to = 'media')

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return "/works/%i/" % self.id

class Slider(models.Model):
    title = models.CharField( max_length = 500)
    image = ThumbnailerImageField( upload_to = 'media')
    whichslide = models.ForeignKey('Portfolio')

    def __unicode__(self):
        return self.title

views.py

class SliderListView(ListView):
    model = Slider
    context_object_name = 'sliders'
    template_name = 'pages/mainslider.html'

def get_context_data(self, **kwargs):
    context = super(SliderListView, self).get_context_data(**kwargs)
    context['portfolio'] = Portfolio.objects.all()
    return context

mainslider.html

<ul class="rslides" id="slider">
{% for slider in sliders %}
  <li>
    <img src="{{ slider.image.mainslider.url }}" alt="{{ slider.whichslide.title }}">
    <p class="caption">{{ slider.title }}</p>
  </li>
{% endfor %}
</ul>

和一些 urls.py

url(r'^slider/$', SliderListView.as_view(), name="slider_list"),

1 个答案:

答案 0 :(得分:0)

就像@Simein Visser所说,你必须在索引视图中包含数据。

可以使用一个简单的解决方案,而不是index函数:

class Index(SliderListView):
    # you're reusing the context data included by SliderListView view
    template_name = 'index.html'