[英]How to define Django view
I am trying to develop a chatbot using Django and depending on the user's input, various python scripts need to be run. 我正在尝试使用Django开发一个聊天机器人,并且取决于用户的输入,需要运行各种python脚本。
Having the project structure (presented below), is there a way to call the news.py in chatbot.js? 具有项目结构(如下所示),有没有办法在chatbot.js中调用news.py?
I have tried with an ajax request: 我已经尝试过ajax请求:
$.ajax({
type: "POST",
url: "news/",
data: { }
}).done(function( o ) {
print('success')
});
and defined news/ in my urls.py 并在我的urls.py中定义了news /
url('news/', getNews)
where getNews is defined in my views 在我的视图中定义了getNews
from artemis.static.marketdata.news import scrape
class getNews():
scrape()
but I get a 500 error saying that TypeError: object() takes no parameters 但出现500错误 ,提示TypeError:object()不带参数
Traceback: 追溯:
Internal Server Error: /news/
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: object() takes no parameters
What would be the preferable approach in this case? 在这种情况下,哪种方法更可取? Any tip would be greatly appreciated!
任何提示将不胜感激!
Django has "function" view and "class" view. Django具有“功能”视图和“类”视图。
You are defining class getNews()
. 您正在定义
class getNews()
。 So you have to choose use "function" view or "class" view. 因此,您必须选择使用“功能”视图或“类”视图。
To use function view, change "class" to "def": 要使用函数视图,请将“ class”更改为“ def”:
from django.http import HttpResponse
def getNews(request):
result = scrape()
return HttpResponse(result)
To use class view, define the "getNews()" in correct way. 要使用类视图,请以正确的方式定义“ getNews()”。
in view: 鉴于:
from django.views import View
from django.http import HttpResponse
class NewsView(View):
...
def get(self, request, *args, **kwargs):
result = scrape()
return HttpResponse(result)
in urls.py: 在urls.py中:
from views import NewsView
...
url(r'^news/$', NewsView.as_view()),
I think your class declaration is wrong. 我认为您的课程声明有误。
class getNews(object):
def __init__(self, *args, **kwargs):
# continue here
def scrape(self):
# continue here
New-style classes inherit from object
, not from NoneType
. 新型类从
object
继承,而不是从NoneType
继承。
Note: this answer addresses a code error in the source, nothing to do with django, just python syntax. 注意:此答案解决了源代码错误,与django无关,仅是python语法。
Samuel Chen's answer addresses how you create a view in django. Samuel Chen的答案解决了如何在Django中创建视图。 A django view is a new-style class, derived from
django.views.View
. django视图是从
django.views.View
派生的新型类。 It contains methods corresponding to the HTTP methods (ie get
, post
, put
etc.). 它包含与HTTP方法相对应的方法(即
get
, post
, put
等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.