在视图/模板中将数据传递到页面的结构

时间:2015-05-21 14:53:09

标签: python django mezzanine

我正在尝试创建一个名为Deals的应用程序,它会在我的主页上创建一个块并显示一堆交易。这是在Django / Mezzanine中创建的。但是,我无法通过模板正确识别字典数据。有人可以解释我做错了什么:

在我的主base.html文件中,我有以下内容:

<div class="col-md-9 middle">
    {% block deals %}
    {% ifinstalled deals %}
    {% include "deals.html" %}
    {% endifinstalled %}
    {% endblock %}
    {% block main %}{% endblock %}
</div>

在我的主文件夹中,我有一个叫做特价的应用。在该文件夹中有一个名为 templates 的子目录,其中包含文件 deals.html 。正确调用此文件,因为下面显示的 else 条件正在呈现给页面。

这是我的deals.html文件:

<div class="row">
        {% if deals %}
                {% for deals in deals %}
                        <div class="deal col-sm-6 col-md-4">
                                <div class="thumbnail">
                                        <a href="{{ deal.url }}" target="_blank"><img alt=" {{ deal.title }}" src="{{ deal.image }}" data-holder-rendered="true" style="height: 200px; width: 100%; display: block;"></a>
                                        <div class="caption">
                                                <h3 id="thumbnail-label"><a href="{{ deal.url }}" target="_blank">{{ deal.title }}</a><a class="anchorjs-link" href="#thumbnail-label"><span class="anchorjs-icon"></span></a></h3>
                                                <h4>{{ deal.price }}</h4>
                                                <p>{{ deal.description }}</p>
                                                <div class="row">
                                                        <div class="col-xs-6">
                                                                <a href="{{ deal.url }}" class="btn btn-primary" role="button" target="_blank">View</a>
                                                                <button type="button" class="favoritebutton btn btn-default btn-md" data-toggle="tooltip" data-placement="right" title="Click to save">
                                                                        <span class="glyphicon glyphicon-heart empty"></span>
                                                                </button>
                                                        </div>
                                                        <div class="col-xs-6">
                                                                <img class="providerlogo" src="{{ deal.provider_img }}" alt="{{ deal.provider }}"/>
                                                        </div>
                                                </div>
                                        </div>
                                </div>
                        </div>
                {% endfor %}
        {% else %}
                <strong>There are no current deals that match your selection. Try expanding your selection and get going!</strong>
        {% endif %}
</div>

在deals / views.py下我有以下内容:

from django.shortcuts import render,render_to_response
from django.http import HttpResponse
from django.template import RequestContext

# Create your views here.
def deals(request,slug):
    """Handles the deals"""
    data = {'deals':[{'title':'5 Days 4 Nights in New York City',
                     'price':'$1799',
                     'description':'Check out the city that never sleeps. Go skating in Rockefeller Center or catch a show on Broadway. This is the trip of a lifetime.',
                     'provider_img':'http://static.example.com/20150502/partners/logo/small/xgroupon.png.pagespeed.ic.B-5dk6Jr_C.png',
                     'provider':'Name',
                     'url':'http://www.example.com/deal1',
                     'img':'http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2d/f2/new-york-city.jpg'},
                     {'title':'Dream trip to the carribbean',
                     'price':'$2199',
                     'description':'Enjoy sun, sand and cocktails. This is a trip of a lifetime to sit back and relax. Forget your busy life and enjoy the carribbean',
                     'provider_img':'http://static.example.com/20150502/partners/logo/small/xgroupon.png.pagespeed.ic.B-5dk6Jr_C.png',
                     'provider':'Name',
                     'url':'http://www.example.com/deal2',
                     'img':'http://i.telegraph.co.uk/multimedia/archive/02464/caribbean_2464021b.jpg'},
                     {'title':'Airfare and Hotel to Paris',
                     'price':'$2400',
                     'description':'It\'s the city of love. Fabulous food, wine and romance in Paris. This trip includes hotel and airfare. Prices are for two or more people",
                     'provider_img':'http://static.example.com/20150502//partners/logo/small/xgroupon.png.pagespeed.ic.B-5dk6Jr_C.png',
                     'provider':'Name',
                     'url':'http://www.example.com/deal3',
                     'img':'http://cache.graphicslib.viator.com/graphicslib/thumbs674x446/2050/SITours/eiffel-tower-paris-moulin-rouge-show-and-seine-river-cruise-in-paris-150305.jpg'}
                     ]
            }
    return render_to_response('templates/deals.html',data,context_instance=RequestContext(request))

目前正在呈现以下内容:

There are no current deals that match your selection. Try expanding your selection and get going!

有谁可以指出我做错了什么?似乎没有识别发送到页面的字典。

修改 如建议的那样,问题似乎是模板标签的问题。我已将以下内容添加到&#39; deals.html&#39;:

的顶部
{% deals %}

并将views.py更改为:

from django.shortcuts import render,render_to_response
from django.http import HttpResponse
from django.template import RequestContext

# Create your views here.
@register.inclusion_tag('deals.html')
def deals():
    """Handles the deals"""
    data = {'deals':[{'title':'5 Days 4 Nights in New York City',
                     'price':'$1799',
                     'description':'Check out the city that never sleeps. Go skating in Rockefeller Center or catch a show on Broadway. This is the trip of a lifetime.',
                     'provider_img':'http://static.example.com/20150502/partners/logo/small/xgroupon.png.pagespeed.ic.B-5dk6Jr_C.png',
                     'provider':'Name',
                     'url':'http://www.example.com/deal1',
                     'img':'http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2d/f2/new-york-city.jpg'},
                     {'title':'Dream trip to the carribbean',
                     'price':'$2199',
                     'description':'Enjoy sun, sand and cocktails. This is a trip of a lifetime to sit back and relax. Forget your busy life and enjoy the carribbean',
                     'provider_img':'http://static.example.com/20150502/partners/logo/small/xgroupon.png.pagespeed.ic.B-5dk6Jr_C.png',
                     'provider':'Name',
                     'url':'http://www.example.com/deal2',
                     'img':'http://i.telegraph.co.uk/multimedia/archive/02464/caribbean_2464021b.jpg'},
                     {'title':'Airfare and Hotel to Paris',
                     'price':'$2400',
                     'description':'It\'s the city of love. Fabulous food, wine and romance in Paris. This trip includes hotel and airfare. Prices are for two or more people",
                     'provider_img':'http://static.example.com/20150502//partners/logo/small/xgroupon.png.pagespeed.ic.B-5dk6Jr_C.png',
                     'provider':'Name',
                     'url':'http://www.example.com/deal3',
                     'img':'http://cache.graphicslib.viator.com/graphicslib/thumbs674x446/2050/SITours/eiffel-tower-paris-moulin-rouge-show-and-seine-river-cruise-in-paris-150305.jpg'}
                     ]
            }
    return data

但是现在我收到以下错误,表明它没有正确注册:

Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 1.6.11
Exception Type: TemplateSyntaxError
Exception Value:    
Invalid block tag: 'deals'

1 个答案:

答案 0 :(得分:0)

您似乎假设在某处包含deal.deals()模板时会自动调用deals.html视图。 这不是它的工作原理 - 在Django中,“views”实际上是请求处理程序,使用HTTP请求调用的iow代码并返回HTTP响应。

问题的解决方案是自定义模板标记 - 在本例中为inclusion_tag(如文档here所述)。好消息:你已经拥有了大部分代码,所以你的问题应该在几分钟后解决。