有人可以帮我弄清楚为什么我的Django模板页面不会呈现任何内容吗?
我正在使用Python请求(http://docs.python-requests.org/en/latest/)从外部网址检索JSON数据。我使用.json()解码数据。这在我从命令行执行此操作时可以正常工作,但是当它在视图中不执行任何操作时。
运行服务器时,页面为空白。它没有标题,没有“测试”打印,没有。
我的模板:
<html>
<head><title>Offer List</title></head>
<body>
<p>Testing</p>
{% load dictparser %}
{% for offers in network1_offers %}
{% autoescape off %}
<div>
<p>name: {{ offers|lookup:"name" }}</p>
<p>pay: {{ offers|lookup:"payout" }}</p>
<p>description: {{ offers|lookup:"description" }}</p>
</div>
{% empty %}
<li>Sorry, no surveys available.</li>
{% endautoescape %}
{% endfor %}
</body>
</html>
我的观点:
class OffersList(View):
template_name="generic_app/offers.html"
def load_offers(request):
"""
import example network offers.
"""
user = request.user
user_agent = request.META['HTTP_USER_AGENT']
amparams = {'user_subid':user.sub_id, 'useragent':user_agent, 'user_ip':user.ip_address}
examplenetwork = requests.get('http://example.com/api-get.php?pubid=00000&key=000000000000&mode=offers&incent=1', data=amparams)
exampleoffers= examplenetwork.json()
"""
import example network 2 offers.
"""
clparams = {'subid':user.sub_id, 'ua':user_agent, 'geoip':user.ip_address}
examplenetwork2 = requests.get('http://www.examplenetwork2.com/blahblah', data=clparams)
exampleoffers2 = examplenetwork2.json()
render(request, 'generic_app/offers.html', {'network1_offers':exampleoffers, 'network2_offers':exampleoffers2})
网址:
url(r'^dashboard/offers$', OffersList.as_view(), name="offers"),
答案 0 :(得分:1)
您之所以看到这一点,是因为您尚未在视图中定义如何使用load_offers()
方法,目前您的load_offers()
方法只是您班级中的一种方法。
使用基类View
附带了您需要实现的方法,例如
class OfferView(View):
template_name = "generic_app/offers.html"
def get(self, request, *args, **kwargs):
return load_offers(request)
或者更好地将其更改为TemplateView
(因为这就是它的真实含义)。
class OfferView(TemplateView):
template_name = "generic_app/offers.html"
def get_context_data(self, **kwargs):
context = super(OfferView, self).get_context_data(**kwargs)
context['offers'] = load_offers(self.request)
return context