"GET /css/styles.css HTTP/1.1" 404 2578 - django

I'm new to Django and this is my first time using it. I keep getting the error "GET /css/styles.css HTTP/1.1" 404 2578" I've already looked through other solutions posted and none have helped. I've followed the documentation exactly as laid out by Django's documentation, but it still isn't working.
Relevant settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIR = [
os.path.join(str(BASE_DIR.joinpath('static')),)
]
index.html
{% load static %}
<html lang="en"><head>
...
<link href="{% static 'css/styles.css' %}" rel="stylesheet"/>
</head>...
Relevant urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App1.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
EDIT: It was a simple fix. I simply forgot to add an "S" at the end of STATICFILES_DIR. It is supposed to be STATICFILES_DIRS.

The problem is probably that your index.html file is in a templates folder and not your main Project1 folder. The code is searching for a file with the path templates/static/css/styles.css, which doesn't exist (404 error). Try moving your index.html file outside of your templates folder and see if it works.

Related

Django Framework got Server Error 500 when DEBUG to False

I am using Django framework for project. I am rendering the HTML templet
def index(request):
print(" --- check fine?") # for print debug
trees = Tree.objects.all()
print(trees)
return render(request, "index.html", {'trees': trees})
--- check fine?
<QuerySet [<Tree: Tree object (8)>, <Tree: Tree object (10)>, <Tree: Tree object (11)>, <Tree: Tree object (12)>]>
[06/Feb/2021 17:28:44] "GET / HTTP/1.1" 500 145
these are my urls.
urlpatterns = [
path("", views.index, name="index"),
path('about/', views.about , name='about'),
path('contact/', views.contact , name='contact'),
path('upload/', views.upload_file , name='upload'),
]
DEBUG = False I am getting above error.
when I set my
DEBUG = True every thing works fine means show index.html and shows even data too.
setting DEBUG = False make difficult to find an error.
index.html contains below code which fetches data.
{% for tree in trees %}
<article class="col-lg-3 col-md-4 col-sm-6 col-12 tm-gallery-item">
<figure>
<img src="{{tree.image.url}}" alt="Image" class="img-fluid tm-gallery-img" />
<figcaption>
<h4 class="tm-gallery-title">{{tree.name}}</h4>
<p class="tm-gallery-description">{{tree.desc}}</p>
<!-- <p class="tm-gallery-price"></p> -->
</figcaption>
</figure>
</article>
{% endfor %}
and allowed hosts are.
ALLOWED_HOSTS = ['*','127.0.0.1','localhost']
Check this out:
Django returns an HTTP status code of 500, an Internal Server Error, when Django encounters runtime errors in a view, such as a syntax error or a view failing to return an object that Django expects. Closely related to errors in view logic is that Django itself will raise the TemplateDoesNotExist exception when an error is encountered in a view, the DEBUG setting is set to False, and a 500.html template is not found.
Beginning with Django 1.5 a new “allowed hosts” setting was introduced. When DEBUG is set to False, it is enforced. In your settings.py file, find the that looks like:
ALLOWED_HOSTS = []
and change it to include the Droplet’s IP address and/or the domain name pointing to your site. For example:
ALLOWED_HOSTS = ["example.com", "111.111.111.111"]
Source:
https://docs.webfaction.com/software/django/troubleshooting.html#:~:text=Django%20returns%20an%20HTTP%20status,an%20object%20that%20Django%20expects.
https://www.digitalocean.com/community/questions/server-error-500-django-digital-ocean
This are varible from settings.py
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
due to STATICFILES_STORAGE collectstatic were using whitenoise engine. I comment STATICFILES_STORAGE .
I suggest to delete all STATIC_ROOT directry in above case /staticfiles folder.
and use
python manage.py collectstatic
to collect files.
and use this in url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('ourhouse.urls')),
path('admin/', admin.site.urls),
# path('accounts/',include('accounts.urls'))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
to use static and media files.

collectstatic collecting static files but they're not loading on webpage

I am deploying my Django site on A2 Hosting.
I can get the page to display but no static files (images/css) are loading.
I have this in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
ROOT_PATH = os.path.dirname(__file__)
STATICFILES_DIRS = [os.path.join(ROOT_PATH, 'static')]
my urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('mysite.urls')),
url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I am running python manage.py collectstatic in the terminal. It is creating a static folder with a file structure including admin and subfolders within that admin folder. My images and css files are appearing in the newly generated static file (in the base folder) yet neither the css nor my images are showing on my webpage (I've been restarting my server).
My link to my css file within my html template is:
<link rel="stylesheet" href="{% static 'style.css' %}" type="text/css">
This is done on shared hosting, I have debug set to false, and it works in development.
/home/mysite
->etc
->mysite
-->__pycache__
-->webpage
--->static
---->webpage
----->image.jpg
----->style.css
--->templates
---->webpage
----->index.html
-->main_webpage
--->settings.py
-->public
-->static_files (generated by collectstatic)
--->admin
--->image1
--->style.css
-->template
-->tmp
->logs
...
Thank you.

Django Add Scond Static Files URL

I am using Django with a Bootstrap template, that requires Jquery. But I am having trouble with a js file.
I created static directory, and static_cdn directory.
I am using a Bootstrap 4 template.
My project template requires a js file (template doesn't working correctly without this js file) but this js file is not using a valid url; It is calling all my svg files, but with a nonvalid URL.
This is my project static files folder :
This is my urls.py urls :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^panel/$', panelView, name='panel'),
url(r'^pageOne/$', pageOne, name='pageOne'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), "static"),
# '/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
MEDIA_URL = '/evrak/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "evrak")
This is my html page :
<!-- BEGIN: Vendor JS-->
<script src="{% static 'app-assets/vendors/js/vendors.min.js' %}"></script>
<script src="{% static 'app-assets/fonts/LivIconsEvo/js/LivIconsEvo.tools.js' %}"></script>
<script src="{% static 'app-assets/fonts/LivIconsEvo/js/LivIconsEvo.defaults.js' %}"></script>
<script src="{% static 'app-assets/fonts/LivIconsEvo/js/LivIconsEvo.min.js' %}"></script>
{% block vendorJS %}
{% endblock vendorJS %}
<!-- END Vendor JS-->
<!-- BEGIN: Page Vendor JS-->
{% block pageVendorJS %}
{% endblock pageVendorJS %}
<!-- END: Page Vendor JS-->
Now,
Django is able to load everything in static directory. And also everything is working correctly.
But, a js file is calling my all svg files , and it is using a nonvalid url.
This is my problematic js file :
"{% static 'app-assets/vendors/js/vendors.min.js' %}"
This is my errors :
You can see 'initiator' field that shows who is calling files.
So, all 'red' files' initiator is 'vendor.min.js'.
If you look, problem is all about this 'vendors.min.js' file. Django is loading everything correctly first, but after loading this js file, it is calling all svg files with a nonvalid url; so browser throws 404 not found error.
Normally everything is correct like that :
Django is loading everything correctly , but this vendors.min.js thing is calling all files in a wrong way, so Django can not find them.
I couldnt find how to change this js file to call a valid address, or add 'app-assets' url directory to Django.
How can I fix this problem ?
I think , if I can add a static url path starts with '/app-assets/' directly , all will work . But for now couldn't find, how to add a second static url path.
Solved.
Added a new path that uses directory, and solved.
This is new settings :
STATIC_URL = '/static/'
STATIC_URL2 = '/app-assets/'
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), "static"),
# '/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
STATIC_ROOT2 = os.path.join(os.path.dirname(BASE_DIR), "static_cdn/app-assets")
MEDIA_URL = '/evrak/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "evrak")
This is new urls.py :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^panel/$', panelView, name='panel'),
url(r'^pageOne/$', pageOne, name='pageOne'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.STATIC_URL2, document_root=settings.STATIC_ROOT2)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now, everything is working.
both
'http://127.0.0.1:8000/app-assets/fonts/LivIconsEvo/svg/share.svg'
and
'http://127.0.0.1:8000/static/app-assets/fonts/LivIconsEvo/svg/share.svg'
links are valid now.

Images from media folder is not displaying django template

I am having images in my media folder and I want to display them but Django is not displaying images other than static folder.
In my setting
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),]
MEDIA_ROOT = BASE_DIR
MEDIA_URL = '/media/'
In my urls.py after url pattern I added this
+ static(MEDIA_URL, document_root=MEDIA_ROOT)
In my templates
<img class="card-img-top" style="" src="{{result.object.thumbnail.url}}" alt=""></a>
<p>{{result.object.thumbnail.url}}</p>
It is showing the correct path but not showing the image, I am unable to figure out the problem. Thanks
Your MEDIA_ROOT have the path of the root of your project. You must join to it, the direcotry of your media. (I suppose media/ is the directory name where you upload all your media)
I think you should have your MEDIA_ROOT like that.
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_ROOT have root path of your project. MEDIA_URL must be correct. Check following
setting.py of your project
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py of project
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url('', include('app1.urls')),
url('home/', myapp_views.index)
] + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I was facing same error from the last 5 days , follow django documentation, many tutorials but nothing work for me but finally I found the solution
I am showing the index.html on home means I have not given any endpoint in url of my app and project url file
app/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', 'views.index', name='index page'),
]
project/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url('', include('app1.urls')),
url('', myapp_views.index)
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Rest of code remains the same and I changed the following in both files
app/urls.py
url('home/', 'views.index', name='index page'),
project/urls.py
url('home/', myapp_views.index)
That's work for me. Accept the answer if also work for you
This is a chunk of my HTTP logs from django v3.2 running wagtail. Looks as though django is trying to tell me where the missing media is but not able to. Because this is a thicket page with gallery sub-images of "featured pages" the view source in my browser doesn't reveal the image file attempt, but I am assuming it is the same issue as OP's with misconfigured MEDIA_ROOT. Strangely, not seeing any errors in the page's linked images when I bring the child page up in wagtail admin. Anyone have an idea why the missing image won't bubble up to the HTTP logs, or what causes the "media/not-found" substitution for the "real" item causing the 404?
[25/Aug/2021 20:28:53] "GET /static/wagtailadmin/images/bg-dark-diag.svg HTTP/1.0" 200 700
Not Found: /media/not-found
[25/Aug/2021 20:29:18] "GET /media/not-found HTTP/1.0" 404 3252
Not Found: /media/not-found
[25/Aug/2021 20:29:18] "GET /media/not-found HTTP/1.0" 404 3252

Django 1.6.5 - 404 error with static files

I know there are other questions similar to mine, but I've tried everything to get my static files working and need some help. When I open my page I get these errors:
GET /static/css/cerulean/css HTTP/1.1" 404 2983
GET /static/js/bootstrap.js HTTP/1.1 404 2980
This is my project directory (most files ommitted):
Motif_Django
Motif_Django
settings.py
urls.py
motif
static
css
cerulean.css
js
bootstrap.js
Here are the important settings in settings.py:
STATIC_URL = 'http://127.0.0.1:8000/static/'
STATIC_ROOT = '/home/kimberly/Motif-Scan-Plus/Motif_Django/static'
STATICFILES_DIRS = (
'/home/kimberly/Motif-Scan-Plus/Motif_Django/static',
'/home/kimberly/Motif-Scan-Plus/Motif_Django/motif/static/motif/',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)
Under INSTALLED_APPS I have included django.contrib.staticfiles. The STATIC_ROOT folder was initially empty and I collected static files with python manage.py collectstatic and I still get these errors. In my HTML form I am using {% load staticfiles %} and trying to load it with
<link rel="stylesheet" type="text/css" href="{% static 'css/cerulean.css' %} but nothing is working. I included the full URL in STATIC_URL to see if that would help, as well as adding the full path of my static folder in STATICFILES_DIRS but still nothing. Any help would be greatly appreciated!!
STATIC_URL is not supposed to be absolute URL, just the path (ie. /static/).
settings.DEBUG has to be True
You have to serve static() url patterns in urls.py
.
urlpatterns = [
# ... patterns,
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Check related guide in Django docs:
https://docs.djangoproject.com/en/dev/howto/static-files/