Skip to content

Commit 84d7923

Browse files
committed
feat(prod): Added custom 404 page and dynamic ALLOWED_HOSTS
1 parent a584c09 commit 84d7923

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

inventory/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,4 +1019,10 @@ def create_user(request):
10191019
else:
10201020
form = UserCreationForm()
10211021
context = {'form': form}
1022-
return render(request, 'inventory/create_user.html', context)
1022+
return render(request, 'inventory/create_user.html', context)
1023+
1024+
def custom_page_not_found_view(request, exception):
1025+
"""
1026+
Custom view to render the 404.html template.
1027+
"""
1028+
return render(request, "404.html", status=404)

sherlock/settings.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
https://docs.djangoproject.com/en/5.1/ref/settings/
1313
"""
1414

15+
import socket
1516
from pathlib import Path
1617

1718
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -25,9 +26,17 @@
2526
SECRET_KEY = 'django-insecure-gspzj&@$^rf!^erfwwj_2zm@thy@q-4sag4xip^8gdm5b-&e6s'
2627

2728
# SECURITY WARNING: don't run with debug turned on in production!
28-
DEBUG = True
29+
DEBUG = False
2930

30-
ALLOWED_HOSTS = []
31+
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
32+
if not DEBUG:
33+
try:
34+
hostname = socket.gethostname()
35+
ALLOWED_HOSTS.append(hostname)
36+
ip_address = socket.gethostbyname(hostname)
37+
ALLOWED_HOSTS.append(ip_address)
38+
except socket.gaierror:
39+
print("Warning: Could not determine hostname or IP for ALLOWED_HOSTS. Network access may be limited.")
3140

3241

3342
# Application definition
@@ -44,7 +53,7 @@
4453

4554
MIDDLEWARE = [
4655
'django.middleware.security.SecurityMiddleware',
47-
'whitenoise.middleware.WhiteNoiseMiddleware',
56+
'whitenoise.middleware.WhiteNoiseMiddleware',
4857
'django.contrib.sessions.middleware.SessionMiddleware',
4958
'django.middleware.common.CommonMiddleware',
5059
'django.middleware.csrf.CsrfViewMiddleware',

sherlock/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.urls import path, include
1515
from inventory import views as inventory_views
1616

17+
1718
urlpatterns = [
1819
path('admin/', admin.site.urls),
1920

@@ -23,4 +24,6 @@
2324
path('accounts/', include('django.contrib.auth.urls')),
2425

2526
path('', include('inventory.urls')),
26-
]
27+
]
28+
29+
handler404 = 'inventory.views.custom_page_not_found_view'

templates/404.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{% extends "inventory/base.html" %}
2+
{% load static %}
3+
4+
{% block content %}
5+
<style>
6+
.error-container {
7+
display: flex;
8+
justify-content: center;
9+
align-items: center;
10+
min-height: 75vh;
11+
text-align: center;
12+
}
13+
.error-wrapper {
14+
position: relative;
15+
max-width: 600px;
16+
width: 100%;
17+
}
18+
.background-404 {
19+
position: absolute;
20+
top: 50%;
21+
left: 50%;
22+
transform: translate(-50%, -50%);
23+
font-size: 20rem;
24+
font-weight: 800;
25+
color: #e2e3e5;
26+
z-index: 1;
27+
line-height: 1;
28+
user-select: none;
29+
}
30+
.content {
31+
position: relative;
32+
z-index: 2;
33+
padding: 2rem;
34+
}
35+
.logo {
36+
width: 100%;
37+
max-width: 300px;
38+
height: auto;
39+
margin-bottom: -1.5rem;
40+
}
41+
.error-title {
42+
font-weight: 700;
43+
font-size: 1.75rem;
44+
margin-top: 2rem;
45+
padding-bottom: 1rem;
46+
border-bottom: 1px solid #585858;
47+
}
48+
</style>
49+
50+
<div class="error-container">
51+
<div class="error-wrapper">
52+
<div class="background-404">404</div>
53+
54+
<div class="content">
55+
<img src="{% static 'inventory/images/logo.svg' %}" alt="Sherlock Logo" class="logo">
56+
<h2 class="error-title">Page Not Found</h2>
57+
58+
<p class="lead text-muted mt-3">
59+
It seems the page you're looking for has vanished into thin air.
60+
</p>
61+
62+
<p>Let's get you back to a familiar place.</p>
63+
64+
<a href="{% url 'inventory:dashboard' %}" class="btn btn-link btn-lg mt-2">
65+
<i class="fas fa-tachometer-alt"></i> Go to Dashboard
66+
</a>
67+
</div>
68+
</div>
69+
</div>
70+
71+
{% endblock %}

0 commit comments

Comments
 (0)