page not found when serving static files via django - python

so in settings.py I have
STATIC_URL = '/static/'
and also
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.apps.MyappConfig'
]
hence as you can see django.contrib.staticfiles is there and moreover Debug = True
and in my app directory I have a file existing in this path
myapp/static/css/myapp.css
However, when I go to http://localhost/myapp/static/css/myapp.css
it ends up returning
Page not found (404)
Request Method: GET
what am I doing wrong and how can I get django to serve static files properly?

Include this in your project/urls.py
from django.conf import settings
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Use the option STATICFILES_DIRS in your settings.
An example:
# settings.py
...
STATICFILES_DIRS = [
"/path/to/your/static/files/folder",
]
...
In your templates, use the static template tag like
# some_page.html
...
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
...
or hard-code like
# some_page.html
...
<img src="/static/my_app/example.jpg" alt="My image"/>
...

Related

Django doesn't see static files

So the thing is that django simply can't recognize the static files. No matter what have I tried it just doesn't work. I tried changing the from global searching way to a direct url of the css file, none of this seems to work.
The structure of my project looks like this:
Here is my code:
DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
from django.contrib import admin
from django.urls import path
from something.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', home),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Monkeys</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/file.css' %}"/>
</head>
Could find the answer in any topic on the internet so I'm hoping you could help.
Go some steps back and set up static files and make sure it follows exactly django docs
Only about static files
This project has similar structure as yours
When collectstatic is run, the default STATICFILES_FINDERS value django.contrib.staticfiles.finders.FileSystemFinder will collect your static files from any paths that you have in STATICFILES_DIRS.
The other default STATICFILES_FINDERS value django.contrib.staticfiles.finders.AppDirectoriesFinder will look in the /static/ folder of any apps in your INSTALLED_APPS.
All of the static files that are found will be placed in the specified STATIC_ROOT directory.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
You can also use python manage.py findstatic to see which directories collectstatic will look in.
manage.py findstatic
Currently you have a typo - STATIC_DIRS instead of STATICFILES_DIRS, so collectstatic is not configured at all currently.
Fix it, then run collectstatic again.

Django 1.11 is not loading css files

I have the next configuration:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^manager/', include('manager.urls')),
#Static pages
url(r'^index', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^contact', TemplateView.as_view(template_name='contact.html'), name='contact'),
url(r'^services', TemplateView.as_view(template_name='services.html'), name='services'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'pf.app.pfs',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "assets"),
#'/var/www/static/',
]
I executed the command:
python manage.py collectstatic
And the files are generated, also I added a css file with a single rule but and executed the command again.
But, in the momento to add it to my html file
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
</head>
in the html I get:
<link rel="stylesheet" href="/static/css/style.css">
And in the terminal:
"GET /static/css/style.css HTTP/1.1" 404 1766
What I have wrong in my static files configuration?

Django Serving Static Files not loading

Setting up a new django application with the following settings:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'kb/media')
STATIC_ROOT = os.path.join(BASE_DIR, 'kb/static/')
Setup a template
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>HomePage</title>
</head>
<body>
<img src="{% static 'img/girl.png' %}">
<img src="/media/boy-512.png">
</body>
</html>
I assume there's an issue with the settings of how it's supposed to get the static files. In this case the directory for the static image girl.png is kb\kb\static\img\girl.png
If I can help you, this is my settings.py file and one example in my html template :
I have a project : MyProject and one application : my_project (inside I have static document > images documents > test.png file)
My settings.py file looks like :
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "my_project/static"),)
And my HTML template file with image looks like :
<img src="{% static 'images/test.png' %}" />
Hopfully to help you
add your project name in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'yourprojectname',]

Django 1.8 Slider Image not displaying

In Django 1.8, I can upload images and they are saved on the path that they should, but when I add <img src="{{ slider.Image.url }}" alt="Slider" /> to template, the images are not displaying. If I want to see only the image like this http://127.0.0.1:8000/static/images/slider/1.jpg in the url the image does appear. But when I go to the home page http://127.0.0.1:8000/home/ it only show me the alt.
Here is my dir tree:
- home
- static
- static/
css
images
slider
js
The images placed properly in slider. CSS and Js working properly.
Here is my settings file
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home'
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = (
'static/'
)
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'static')
)
When I use static_root the problem get more serious nothing worked.
Here is my models in home for slider
class Slider(models.Model):
ImageName = models.CharField(max_length=200)
Image = models.ImageField(upload_to="images/slider")
Here is my main urls
from django.conf.urls import include, url, patterns
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^home/', include('home.urls')),
]
from django.conf import settings
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)
Here is my home app urls
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^$', views.slider, name='slider',)
]
Here is my template source
{% for slider in sliders %}
<li class="item"><img src="{{ slider.Image.url }}" alt="Slider" /><span class="overlay1"></span></li>
{% endfor %}
You shouldn't mix static and media files. Static files are CSS, JavaScript, Images and other files that comes in package with your app, so it can work properly or to look nice.
Media files are files uploaded from website or just files associated with some objects in database.
That files shouldn't be mixed.
Also, STATIC_ROOT and STATICFILES_DIRS shouldn't point at same directory. All files from STATICFILES_DIRS and files from static directories inside each app will be copied or linked into STATIC_ROOT when ./manage.py collectstatic is executed.
Django development server by default won't serve any media files, if you want them to be served, add to your main urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django can't find the files in static folder

I must be missing something.
So this should be how my folder does look:
Obviously with the asked image.
To increase portability, I use dynamic path in the code.
The code in settings.py
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
STATIC_PATH,
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'rango'
urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),url(r'^rango/', views.about, name='about'))
views.py
def index(request):
context = RequestContext(request)
# Construct a dictionary to pass to the template engine as its context.
# Note the key boldmessage is the same as {{ boldmessage }} in the template!
context_dict = {'boldmessage': "I am bold font from the context"}
# Return a rendered response to send to the client.
# We make use of the shortcut function to make our lives easier.
# Note that the first parameter is the template we wish to use.
return render_to_response('rango/index.html', context_dict, context)
def about(request):
return HttpResponse("Rango says: This is about page!<a href='../../'>Index</a>")
Note that the image is put on a html template. The html is ok (loaded), but the image dont.
<!DOCTYPE html>
{% load static %} <!-- New line -->
<html>
<head>
<title>Rango</title>
</head>
<body>
<h1>Rango says...</h1>
hello world! <strong>{{ boldmessage }}</strong><br />
About<br />
<img src="{% static "rango.png" %}" alt="Picture of Rango" /> <!-- New line -->
</body>
</html>
Result?
The image got returned by a nasty 404
Accessing from 127.0.0.1:8000/static/rango.png or manage.py findstatic rango.png will result a failure.
However, accessing from file:///D:/tango_with_django_project/static/rango.png will be successful.
Thanks
Aditional information:
DEBUG=TRUE
I had checked STATIC_PATH, file did lead to the right path D:\tango_with_django_project\static
Is it possible due to backslash and forward slash, but if the slash is the problem, then the template should also broken.
In production static files should be served by the web server and not by django itself. That's why it is not enabled by default.
You need to say to the development server that it must serve the static files. I think that setting DEBUG=True in your settings.py should be enough.
You may also need to add this in your urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'your_app.views.home', name='home'),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls))
)
if settings.DEBUG :
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^static/(?P<path>.*)$', 'serve'),
)
I hope it helps
The problem may have to do with a lack of finders for the app's directory. Add the following to the project's setting.py:
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

Categories