2017-07-27 70 views
1

我試圖在Django項目中使用Goodreads API。我已經設置了一個簡單的標籤來提取reviews_widget,並試圖將CSS樣式和html發送到模板。但是,樣式和html在發送時沒有被註冊。使用{{reviews_widget}}將樣式和html顯示爲字符串,但{%loading reviews_widget%}不顯示任何內容。Goodreads API和Django

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} 

由於風格需要在頭部分,我想我會必須將響應分成不同的變量。我怎麼能這樣做呢?另外,模板如何將響應視爲html而不是字符串?

回答

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> 
+0

好東西。謝謝。只是爲了澄清,simple_tag不是必要的。我可以在views.py中設置變量併發送到模板。 – limaBEAN

+0

Yepp!如果你想用模板標籤格式化數據,那也可以做到。但是,如果您的視圖按照您希望在模板內部使用HTML的方式精確分割HTML響應,則無需執行以下操作:只需添加|安全篩選器,它就會像您期望的那樣進行渲染。 –