0

I am trying to develop a chatbot using Django and depending on the user's input, various python scripts need to be run.

Having the project structure (presented below), is there a way to call the news.py in chatbot.js?

I have tried with an ajax request:

$.ajax({
  type: "POST",
  url: "news/",
  data: { }
}).done(function( o ) {
   print('success')
});

and defined news/ in my urls.py

url('news/', getNews)

where getNews is defined in my views

from artemis.static.marketdata.news import scrape

class getNews():
    scrape()

but I get a 500 error saying that TypeError: object() takes no parameters

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!

enter image description here

7
  • Which line raised the exception? what is scrape, function or object ? Commented Jul 27, 2018 at 7:19
  • @SamuelChen I have edited my answer to include the traceback, and scrape is a function in news.py Commented Jul 27, 2018 at 7:23
  • you may need to use function view "def" not "class" Commented Jul 27, 2018 at 7:41
  • Please read this stackoverflow.com/help/how-to-ask Commented Jul 27, 2018 at 7:44
  • yup, maybe "How to define Django view" is a better question title for this thread. Commented Jul 27, 2018 at 7:47

2 Answers 2

3

Django has "function" view and "class" view.

You are defining class getNews(). So you have to choose use "function" view or "class" view.

To use function view, change "class" to "def":

from django.http import HttpResponse


def getNews(request):
    result = scrape()
    return HttpResponse(result)

To use class view, define the "getNews()" in correct way.

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:

from views import NewsView
...
url(r'^news/$', NewsView.as_view()),
Sign up to request clarification or add additional context in comments.

Comments

1

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.


Note: this answer addresses a code error in the source, nothing to do with django, just python syntax.

Samuel Chen's answer addresses how you create a view in django. A django view is a new-style class, derived from django.views.View. It contains methods corresponding to the HTTP methods (i.e. get, post, put etc.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.