Python/Django - 获取请求的数据在临时存储中未出现

2024-04-20 09:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,正在尝试从导入的newsapi中提取数据,但是,尽管没有看到任何错误,但它似乎没有显示在模板中。我正在尝试一个MVC框架来组织我的代码

views.py开始

未来导入unicode\u文本

import json
import logging
import requests
from django.http import HttpResponse
from django.template import loader
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.views import generic
from newsapi import NewsApiClient

from .forms import UserProfileCreationForm
from .models import Animals

def news_list(request):
    newsapi = NewsApiClient(api_key='123')
    all_articles = newsapi.get_everything(
    q='animals',
    sources='bbc-news,national-geographic, new-scientist, reddit-r-all',
    from_param='2018-09-29',
    to='2018-09-22',
    language='en',
    sort_by='relevancy')['articles']
    context = {'articles': all_articles,
           'type': str(type(all_articles)),
           'name': 'Someone'}

    template = loader.get_template('home.html')

    return HttpResponse(template.render(context, request))

然后urls.py

from django.conf.urls import url, include
from . import views

urlpatterns = [
  url(r'^$', views.news_list, name="news_list"),
]

这里还有来自项目文件夹的urls.py,不确定它是否有用

from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic.base import TemplateView


urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
   url(r'^users/', include('dangerzone.urls')),
   url(r'^users/', include('django.contrib.auth.urls'))
]

最后是模板

base.html

<!DOCTYPE html>
 <html>
  <head>
   <meta charset="utf-8">
    <title>
     {% block title %}
      Python App
     {% endblock title %}
    </title>
   </head>
   <body>
    <main>
     {% block content %}

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

home.html

{% extends 'base.html' %}

 {% block content %}

   <h2>Latest News</h2>
    <ul>
     {% for article in articles %}
      <li>{{ article.source }}</li>
      <li>{{ article.author }}</li>
      <li>{{ article.title }}</li>
     {% endfor %}
    </ul>
 {% endblock content %}

如果需要更多的信息请告诉我。你知道吗

到目前为止,我已经尝试了:

  1. 将url地址从r'^$'更改为仅''
  2. 在没有HttpResponse的情况下导入,如下所示:

     def news_list(request):
         newsapi = NewsApiClient(api_key='123')
         all_articles = newsapi.get_everything(
              q='animals',
              sources='bbc-news,national-geographic, new-scientist, reddit-r-all',
              from_param='2018-09-29',to='2018-09-22',
              language='en',
              sort_by='relevancy',
              page=2)['articles']
        return render(request, 'home.html', {'articles' : all_articles})
    

Tags: djangofromimporturlhometitlehtmltemplate
1条回答
网友
1楼 · 发布于 2024-04-20 09:32:54

你能告诉我们你的Python/Django版本吗?你知道吗

另外,如果我没弄错的话,你的

template = loader.get_template('home.html')

return HttpResponse(template.render(context, request))

news_list()函数之外。你知道吗

相关问题 更多 >