I was new on using django. So, i just learn about how to using static assets in django. I already configuring the settings.py with correct configuration for development stage. I also put static folder within the app folder. But, when i running the application, it cannot find the static. Here is my image of my working directory
-project
-my app
-static
-project
here is my settings.py file
STATIC_URL = ‘static/’
MEDIA_URL = ‘images/’
STATICFILES_DIRS = (
os.path.join(BASE_DIR, ‘static’),
)
print(BASE_DIR)
and here is when i called it on my html
{% load static %}
Asian Tours Web Page
Welcome To Asia Tour Web Application
{% for item in items %}
- Origin Country : {{ item.origin_country }}
- Destination Country : {{ item.destination_country }}
- Long Night : {{ item.long_night }}
- Price : ${{ item.price }}
{% endfor %}
the error looks like this
Not Found: /static/css/styles.css
[14/Aug/2024 15:24:50] "GET /static/js/script.js HTTP/1.1" 404 2160
[14/Aug/2024 15:24:50] "GET /static/js/script.js HTTP/1.1" 404 2160
[14/Aug/2024 15:24:50] "GET /static/images/dlogo.png HTTP/1.1" 404 2172
this is output when i print base directory which is already correct
PS D:\Second\Coding\Learn Django\basic-django\myproject\myproject> python settings.py
D:\Second\Coding\Learn Django\basic-django\myproject
any idea why i am facing this problem
Thanks for all kind reply
Hello @ivanpahlevi8 !
Can you include the part of the template that is referencing the static file, and show the full working directory for project\static.
hello cliff,
this is my html template that take reference on static file :
{% load static %}
Asian Tours Web Page
Welcome To Asia Tour Web Application
{% for item in items %}
- Origin Country : {{ item.origin_country }}
- Destination Country : {{ item.destination_country }}
- Long Night : {{ item.long_night }}
- Price : ${{ item.price }}
{% endfor %}
I’m sorry i still cannot upload image so here full directory i can show to you
-myproject
-my app
-static
-templates
-urls.py
-views.py
-myproject
-settings.py
manage.py
{% 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 'css/styles.css' %}">
<title>Asian Tours Web Page</title>
</head>
<body>
<h1>Welcome To Asia Tour Web Application</h1>
<ul>
{% for item in items %}
<li>Origin Country : {{ item.origin_country }}</li>
<li>Destination Country : {{ item.destination_country }}</li>
<li>Long Night : {{ item.long_night }}</li>
<li>Price : ${{ item.price }}</li>
{% endfor %}
<img src="{% static 'images/dlogo.png' %}" alt="Django Logo">
<script src="{% static 'js/script.js' %}" defer></script>
</ul>
</body>
</html>
i am sorry, but that is my html file
Have you placed your static files in the directories you are fetching them from. Does script.js reside in static/js/ folder, like this
project
myapp
—static
------js
----------script.js
The html and settings look fine to me, but I might be missing something obvious
yes, i put my working directory just like that
I’m trying using anaconda for my development environment, and it suddenly solve the problem. i think the problem is related to environment that i used for development.
Do you have debug set to False
in your settings. If so, Django will not serve them.
1 Like
Welcome @ivanpahlevi8 !
For clarification, are you talking about having this issue when trying to deploy your application into a production environment? Or are you continuing to have this issue in your development environment?
Be aware that how static files should be handled in a production environment is completely different from how it’s done in development, and what you do in development has nothing to do with how they’re served in a production environment. (This is one of those topics that many people struggle with in their first production usage of a Django project.)
It seems you are using debug mode.
Did you add the following to urls.py?
if settings.DEBUG: # 디버그 모드인지 확인
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # static url 추가
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # media url 추가
Sorry for my late reply, i already set it to true for development process
I am talking abaout developmenet env which why i set debug to true. Thanks for advice
I haven’t tried to add that in my url, but so far the application is running nicely in my anaconda environment. thanks for the advice
In debug mode, you must add a static URL.