Been suck on this problem for multiple days now. My main page loads fine, but then when i click a button that’s supposed to take me to index.html i get the error posted below. When running this locally it works fine, but when deploying onto AWS (EC2, using nginx and gunicorn) its not working and giving me the error below. I’m new to using Django and AWS, any feedback is appreciated.
Error:
Request Method: GET
Request URL: http://IP/index.html
Using the URLconf defined in LectureLingo.urls, Django tried these URL patterns, in this order:
[name='index']
admin/
The current path, index.html, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
views.py code
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def index(request):
return render(request, 'index.html')
Main urls.py code
from django.conf.urls import include
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('', include('transcript.urls')),
path('index/', include('transcript.urls')),
path('admin/', admin.site.urls),
]
urls.py inside of transcript:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index_view, name='home'),
path('index/', views.index_view, name='index'),
]
Html homepage code
{% load static %}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'homestyles.css' %}">
<title>Home Page</title>
</head>
<body>
<div class="text">LectureLingo</div>
<div class="container">
<div class="dropdown-container">
<div class="dropdown-label">
<div class="label">Translate from:</div>
<select class="dropdown" id="dropdown1" required>
<option value="" disabled selected hidden>Select...</option>
<option value="cs">Czech</option>
</select>
</div>
<div class="dropdown-label">
<div class="label">Translate To:</div>
<select class="dropdown" id="dropdown2" required>
<option value="" disabled selected hidden>Select...</option>
<option value="af">Afrikaans</option>
</select>
</div>
</div>
<button id="sendButton" onclick="checkDropdowns()">Start</button>
</div>
<script src="{% static 'homeJS.js' %}"></script>
</body>
</html>
Main Javascript (only button):
document.querySelector('#sendButton').onclick = function() {
// Check if both dropdowns have selections
var dropdownFrom = document.getElementById('dropdown1').value;
var dropdownTo = document.getElementById('dropdown2').value;
if (dropdownFrom !== "" && dropdownTo !== "") {
// Send data through WebSocket
websocket.send(JSON.stringify({
'dropdown1': dropdownFrom,
'dropdown2': dropdownTo
}));
localStorage.setItem('detectedLanguage', dropdownTo);
window.location.href = 'index.html';
} else {
alert('Please select options from both dropdowns.');
}
};
django.conf:
server {
listen 80;
server_name My IP;
# Define the location for static files
location /static/ {
alias /home/ubuntu/LectureLingo/staticfiles/;
}
# Define how to pass Django requests
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/LectureLingo/app.sock;
}
}
Installed apps (from settings.py):
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = my key
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'channels',
'transcript',
'LectureLingo',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'LectureLingo.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "transcript/templates/")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'LectureLingo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/LectureLingo/staticfiles/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
ASGI_APPLICATION = 'LectureLingo.asgi.application'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': '/home/ubuntu/LectureLingo/logs/django_errors.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': True,
},
},
}
My directory layout:
LectureLingo/
├── LectureLingo/
│–├── settings.py
│–├── asgi.py
│–├── wsgi.py
│–└── urls.py
├── transcript/
│–├── tests.py
│–├── apps.py
│–├── views.py
│–└── templates/
│----├── home.html
│----└── index.html
│–└── urls.py
├──static/
–├── homestyles.css
–└── homeJS.js
–└── indexJS.js
–└── indexstyles.js
└── staticfiles/
–└── admin
----└── css
--------└── more files
----└──js
--------└── more files
----└── fonts
--------└── more files