如何使用 Reddit API 渲染我的 HTML 文件中的图像

How can I use the Reddit API to render images in my HTML file

我正在尝试使用 Python Reddit API Wrapper (Praw) 获取 subreddit 的 posts,然后显示 post 在我的 HTML 中,使用 Django。

我正在使用 Django 2.1.5,Python 3.6.3。我已经能够在单独的测试 python 文件中成功获取图像的 URL,它只是将 URL 打印到控制台。现在我想弄清楚如何使用 django 以便我可以在 HTML.

中显示图像

views.py

from django.shortcuts import render, praw, requests

def subRedditImage(request):
reddit = praw.Reddit(client_id='stuff', 
                    client_secret='stuff', 
                    user_agent='RedditScroller by XXX')
    subreddit = reddit.subreddit('FoodPorn')
    submissions = subreddit.hot(limit = 10)



    return render(request, 'scrollapp/base.html', submissions)

base.html

{% block content%}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, 
    shrink-to-fit=no">

    <title>Reddit</title>

    <h1>Hot posts</h1>
    <img src="{{ submissions }}" alt="">

  </body>
</html>
{% endblock content %}

我想在我的 html 页面上显示从 reddit api 获得的图像。

我在尝试加载页面时收到错误消息:

Traceback:

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\J\Desktop\redditscroll\scrollapp\views.py" in subRedditTitle
  30.     return render(request, 'scrollapp/base.html', submissions)

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader.py" in render_to_string
  62.     return template.render(context, request)

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py" in render
  59.         context = make_context(context, request, autoescape=self.backend.engine.autoescape)

File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\context.py" in make_context
  270.         raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)

Exception Type: TypeError at /
Exception Value: context must be a dict rather than ListingGenerator.


第一个:从不 public只post你的密钥。有了那把钥匙,我可以以你的名义做各种事情。您应该将其视为您的密码。不要将其提交到 public 存储库,也不要将其 post 提交到 Whosebug。

答案:

线索就在错误中。您正在为模板提供 ListingGenerator 而它需要一本字典。您的 submissions 变量引用的对象不是字典。

您应该从生成器中获取所需的数据,并将其添加到字典中,然后再将其传递给模板。

这方面的文档是 here

您可以像这样获取 url 图片:

    reddit = praw.Reddit(client_id='DONTPOSTTHIS',
                         client_secret='KEEPTHISASECRET',
                         user_agent='RedditScroller by XXX')
    subreddit = reddit.subreddit('FoodPorn')

    context = {}
    urls = []

    for submission in subreddit.hot(limit=10):
        urls.append(submission.url)

    context['urlList'] = urls
    print(context)

您应该将该上下文传递给您的模板并以您想要的任何方式使用数据。首先将 {{ submissions }} 替换为 {{ urlList.0 }} ,然后找出如何循环遍历 django 模板中的列表并连续显示所有 'hot' 图像。