Search code examples
pythondjangochatterbot

How to get urls and views correct with this code


Hey guys I'm trying to set up my chatterbot to work with Django but for some reason I can't seem to get the urls and views correct for Django to show the chatbot on my server. Django 2.1.1 is the version I'm running on with Python 3.7 as my interpreter. My chatbot is in the same project in a folder called Sili with it's own views.py and urls.py in that folder.

I have tried this but no luck

    from django.contrib import admin
    from sili import views

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', home.views),
    ]

this is what is in my views.py 

    from django.shortcuts import render,render_to_response
    from django.http import HttpResponse
    import json
    from django.views.decorators.csrf import csrf_exempt

    from chatterbot import ChatBot
    # from chatterbot.trainers import ChatterBotCorpusTrainer
  chatbot=ChatBot('Sili',trainer='chatterbot.trainers.ChatterBotCorpusTrainer')

# Train based on the english corpus
chatbot.train("chatterbot.corpus.english")



@csrf_exempt
def get_response(request):
    response = {'status': None}

    if request.method == 'POST':
        data = json.loads(request.body)
        message = data['message']

        chat_response = chatbot.get_response(message).text
        response['message'] = {'text': chat_response, 'user': False, 'chat_bot': True}
        response['status'] = 'ok'

    else:
        response['error'] = 'no post data found'

    return HttpResponse(
        json.dumps(response),
            content_type="application/json"
        )


def home(request, template_name="home.html"):
    context = {'title': 'Sili Chatbot Version 1.0'}
    return render_to_response(template_name, context)

what do I add to the urls.py so that it shows on the server? This is what I have so far

from django.contrib import admin
from sili import views

urlpatterns = [
    path('admin/', admin.site.urls),
]

Solution

  • The module is views, and the function is home, so:

    from django.contrib import admin
    from sili import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', views.home),  # <-
    ]