2015-05-04 49 views
0

我想在django中創建和管理滑塊。我可以在管理頁面創建,但是當我在主頁上顯示圖像時,它不顯示任何內容。滑塊不在Django中工作1.7

這是我的models.py

class Static(models.Model): 
title   = models.CharField(max_length=50) 
description  = models.TextField() 
images1   = models.ImageField(upload_to="slider") 
images2   = models.ImageField(upload_to="slider") 

def __str__(self): 
    return self.title; 

view.py

def slider(request): 
sliders = Static.objects.all() 
return render(request, 'kerajinan/home.html', { 
'sliders' : sliders, 
}) 

home.html的

<div class="carousel-inner"> 
         {% for i in image %} 
          <div class="item active"> 
           <div class="col-sm-6"> 
            <h2>{{i.title}}</h2> 
            <p>{{i.description}}</p> 
            <button type="button" class="btn btn-default get">Get it now</button> 
           </div> 
           <div class="col-sm-6"> 
            <img src="{{ i.images1.url }}" class="girl img-responsive" alt="" /> 
            <img src="{{ i.images2.url }}" class="pricing" alt="" /> 
           </div> 
          </div> 
         {% endfor %} 

         {{slider|pprint}} 

        </div> 

settings.py:

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images/') 
MEDIA_URL = '/static/images/' 

Urls.py:

urlpatterns = patterns('', 
    url(r'^home/$', 'kerajinan.views.home', name='home'), 
    url(r'^static/images/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}) 
) 

我已與此

python manage.py shell 
>>>from kerajinan.models import Static 
>>>sliders = Static.objects.all() 
>>>sliders 

檢查數據和結果記錄數據。 你能幫我解決這個問題嗎?

回答

3

您不需要編寫自定義方法來獲取圖像的URL。您可以直接在模板中訪問圖片網址:

{{ i.images1.url }} 
+0

第一次我使用{{i.images1.url}}但它不工作,所以我自定義方法來獲取URL。 – User0511