Skip to content

Commit 57d2ddb

Browse files
committed
Waypoint 2
1 parent 0c9846a commit 57d2ddb

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from django.urls import path
1+
from django.urls import path
2+
23
from . import views
34

45
urlpatterns = [
5-
path('', views.index, name='index'), path('api', views.api, name='api'),
6-
]
6+
path('api', views.api, name='api'),
7+
path('', views.index, name='index'),
8+
]

chapter-14/shakespearebot-waypoint-2/bot/views.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@
33
from bot.models import Text
44
import random
55
import json
6+
from django.shortcuts import render
67

78

89
def index(request):
9-
template = loader.get_template("bot/index.html")
10-
11-
return HttpResponse(template.render())
12-
10+
return render(request, "../react-frontend/build/index.html")
1311

1412
def api(request):
15-
if request.method == 'POST':
16-
data = json.loads(request.body.decode("utf8"))
17-
query = data['chattext']
18-
responses = Text.objects.filter(PlayerLine__contains=" %s " % (query))
19-
20-
if len(responses) > 0:
21-
return HttpResponse(responses[random.randint(0, len(responses))])
13+
query = json.loads(request.GET['chattext'])
14+
responses = Text.objects.filter(PlayerLine__contains=" %s " % (query))
2215

23-
else:
24-
return HttpResponse("Get thee to a nunnery!")
16+
if len(responses) > 0:
17+
return HttpResponse(responses[random.randint(0, len(responses))])
18+
else:
19+
return HttpResponse("Get thee to a nunnery!")

chapter-14/shakespearebot-waypoint-2/shakespearebot/settings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
TEMPLATES = [
5656
{
5757
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58-
'DIRS': [],
58+
'DIRS': [os.path.join(BASE_DIR, 'react-frontend')],
5959
'APP_DIRS': True,
6060
'OPTIONS': {
6161
'context_processors': [
@@ -119,4 +119,7 @@
119119
# https://docs.djangoproject.com/en/3.0/howto/static-files/
120120

121121
STATIC_URL = '/static/'
122-
STATIC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
122+
STATICFILES_DIRS = (
123+
os.path.join(BASE_DIR, 'react-frontend', 'build', 'static'),
124+
os.path.join(BASE_DIR, 'react-frontend', 'public'),
125+
)

chapter-14/shakespearebot-waypoint-2/shakespearebot/urls.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
1616
from django.contrib import admin
17-
from django.urls import path, include
18-
import bot
17+
from django.urls import include, path
1918

2019
urlpatterns = [
21-
path('admin/', admin.site.urls),
22-
path('api/', include('bot.urls')),
23-
path('', include('bot.urls')),
20+
path('admin/', admin.site.urls),
21+
path('api/', include('bot.urls')),
22+
path('', include('bot.urls')),
2423
]

0 commit comments

Comments
 (0)