-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_django_static_files__debug_false.py
More file actions
68 lines (47 loc) · 1.5 KB
/
python_django_static_files__debug_false.py
File metadata and controls
68 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
this is not a runnable file,
but is very useful for those,
who are working with the
PYTHON/DJANGO framework.
here is an example code to
run your django app safely
in the production...
"""
## project's settings.py file
# somewhere near to the top of the file
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# so now, you have to set your allowed host(s)
ALLOWED_HOSTS = []
# for example: ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
# at the bottom of the file
# Static files (CSS, JavaScript, Images)
STATIC_URL = "/static/"
MEDIA_URL = "/static/images/"
STATIC_ROOT = BASE_DIR / "assets"
MEDIA_ROOT = BASE_DIR / "static/images"
STATICFILES_DIRS = [
BASE_DIR / "static",
]
# of course, you can give other names as well... this is just an example
## project's urls.py file
from django.conf import settings
from django.views.static import serve
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf.urls.static import static
urlpatterns = [
re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
path("admin/", admin.site.urls),
path('home/', include('home.urls')),
]
"""
so, that's it!
don't forget to run:
python manage.py collectstatic
(in this case, it will collect to the assets folder/directory)
and you don't have to use any third-party tools.
of those the well known and widely used:
whitenoise
"""