【问题标题】:TypeError at /news/ , html() takes no arguments/news/ 处的 TypeError , html() 不带参数
【发布时间】:2014-07-30 18:25:43
【问题描述】:

我是 Django 的初学者。你们能告诉我为什么我会收到这个错误。 代码:

enter code here:
import feedparser
from django.http import HttpResponse

def news():
    YahooContent = feedparser.parse ("http://news.yahoo.com/rss/")
    for feed in YahooContent.entries:
        print feed.published
        print feed.title
        print feed.link + "\n"
   return

def html():
    html = "<html><body> %s </body></html>" % news()
    return HttpResponse(html)

错误:

TypeError at /news/
html() takes no arguments (1 given)
Request Method: GET
Request URL:    this is not a url : (http://djangodefault.com:8000/news/)
Django Version: 1.6.5
Exception Type: TypeError
Exception Value:    
html() takes no arguments (1 given)

【问题讨论】:

    标签: python django python-2.7 django-forms


    【解决方案1】:

    根据documentation:

    视图函数,或简称视图,只是一个 Python 函数 接受一个 Web 请求并返回一个 Web 响应。

    换句话说,您的函数应该接受 request 参数:

    def html(request):
        html = "<html><body> %s </body></html>" % news()
        return HttpResponse(html)
    

    【讨论】:

    • @AmirNakhostin 好的,您的 news() 函数没有返回任何内容。
    • 不应该打印三行吗?我该如何解决?
    • third part of the Dajango tutorial 涵盖了使用模板创建 HTML 页面。
    • @AmirNakhostin 这个答案已经解决了问题,请采纳。
    猜你喜欢
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多