Goodreads API和Django

时间:2017-07-27 15:40:51

标签: python html css django api

我正在尝试在Django项目中使用Goodreads API。我已经设置了一个简单的标签来提取reviews_widget并尝试将css样式和html发送到模板。但是,样式和html在发送时没有注册。使用{{reviews_widget}}将样式和html显示为字符串,但{%load reviews_widget%}不显示任何内容。

book.html

{% extends "base.html"%}


{% block content %}

<!--  Does not render -->
  {% load reviews_widget %}

<!--  Shows response in string format -->
  {{ reviews_widget }}

 <!--  What is sent to template-->
<!--
<style> #goodreads-widget { font-family: georgia, serif; padding: 18px 0; width:565px; }
#goodreads-widget h1 { font-weight:normal; font-size: 16px; border-bottom: 1px solid #BBB596;
margin-bottom: 0; } #goodreads-widget a { text-decoration: none; color:#660; }
iframe{ background-color: #fff; } #goodreads-widget a:hover { text-decoration: underline; }
#goodreads-widget a:active { color:#660; } #gr_footer { width: 100%; border-top: 1px solid #BBB596;
text-align: right; } #goodreads-widget .gr_branding{ color: #382110; font-size: 11px;
text-decoration: none; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; } </style>
<div id="goodreads-widget"> <div id="gr_header"><h1><a href="https://www.goodreads.com/book/show/31430654-the-millionaire-booklet">
The Millionaire Booklet Reviews</a></h1></div> <iframe id="the_iframe"
src="https://www.goodreads.com/api/reviews_widget_iframe?did=DEVELOPER_ID&amp;format=html&amp;isbn=0990355454&amp;links=660&amp;min_rating=&amp;review_back=fff&amp;stars=000&amp;text=000"
width="565" height="400" frameborder="0"></iframe> <div id="gr_footer"> <a class="gr_branding"
target="_blank" href="https://www.goodreads.com/book/show/31430654-the-millionaire-booklet?utm_medium=api&amp;utm_source=reviews_widget">
Reviews from Goodreads.com</a> </div> </div>
-->

{% endblock %}

views.py

from django.shortcuts import render
from givegetapp.models import Give, Get, Reader

# For GoodReads Api
from django.http import HttpResponse
from django.template import Context, loader
from django.http import HttpRequest
import xml.etree.ElementTree as ET
import sys
import requests

from django import template
register = template.Library()


try:
    import urllib.request as urllib2
except ImportError:
    import urllib2

def book():


    isbn = '0990355454'

    key = 'devKEY' #replaced w/ dev key

    response = requests.get('https://www.goodreads.com/book/isbn/'+isbn+'?key='+key)

    tree = ET.fromstring(response.content)

    reviews_widget = str

    for leaves in tree[1]:

        if leaves.tag == "reviews_widget":
            print(str(leaves.text))
            reviews_widget = str(leaves.text)

    return render(request, "book.html", {"reviews_widget": reviews_widget})

templatetags / reviews_widget.py

from django import template

# For GoodReads Api
from django.http import HttpResponse
from django.template import Context, loader
from django.http import HttpRequest
import xml.etree.ElementTree as ET
import sys
import requests

from django import template
register = template.Library()


try:
    import urllib.request as urllib2
except ImportError:
    import urllib2
# end of GoodReads


register = template.Library()

@register.simple_tag()
def reviews_widget():
    isbn = '0990355454'

    key = 'g6mDefuBPg0ajE7c4Nwyg' #replace it with your developer key

    response = requests.get('https://www.goodreads.com/book/isbn/'+isbn+'?key='+key)

    tree = ET.fromstring(response.content)

    reviews_widget = str

    for leaves in tree[1]:

        if leaves.tag == "reviews_widget":
            # print(str(leaves.text))
            reviews_widget = str(leaves.text)
    # print(reviews_widget)
    return {'reviews_widget': reviews_widget}

由于样式需要在head部分,我想我必须将响应拆分为不同的变量。我该怎么做呢?此外,模板如何将响应视为html而不是字符串?

1 个答案:

答案 0 :(得分:2)

<强>分裂 你可以这样做:

first_part = response_html.split("</style>")[0] + "</style>"
second_part = response_html.split("</style>")[1]

然后将first_part和second_part都用作模板上下文变量

return render(request, "book.html", {
    "style_section": first_part,
    "content_section": second_part,
    "reviews_widget": reviews_widget,
})

呈现为HTML而不是字符串 您可以使用mark_safe将其显示为模板中呈现的HTML。如果你能够做到这一点,请仔细考虑,因为如果你不相信数据来源可能会带来安全风险(GoodReads是一个非常安全的选择,但是值得考虑,取决于你的应用程序需要的水密程度)

<html>
    <head>
        {{ style_section|safe }}
    </head>
    <body>
        {{ content_section|safe }}
    </body>
</html>