I'm trying to run my very first project using Python/Django on Openshift and have problem with loading my static files.
I've read the https://docs.djangoproject.com/en/dev/howto/static-files/ multiple times I have been breaking my head over this for a full day but can't figure out the problem.
I'm running a developer server:
python manage.py runserver
setting.py
STATIC_URL = '/static/'
if 'OPENSHIFT_REPO_DIR' in os.environ:
STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'), 'wsgi', 'static')
else:
STATIC_ROOT = os.path.join(WSGI_DIR, 'static')
template
{% load static %}
<img src="{% static "logo2.png" %}">
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import RedirectView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/index/')),
url(r'^index/', include('index.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
urlpatterns += staticfiles_urlpatterns()
The weirdest thing is that after pushing my app to openshift everything is working just fine but on localhost sth goes wrong.
Make long story short:
127.0.0.1:8000/static/logo2.png - Not found
mysite.rhcloud.com/static/logo.png - Yeah, I see the image
I hope it's clear and my problem is as stupid as I imagine.
Django 1.8, OS Windows
SOLUTION:
I've deleted the 'django.contrib.staticfiles' from INSTALLED_APPS and add to the urls.py this peace of code:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Are you in development mode? If yes and if you don't have django.contrib.staticfiles in INSTALLED_APPS, you need to add this to urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Explained here.
Related
Why I am receiving files can't be found error while loading all the static files even after setting everything correctly.
ERROR
CODE:
Admin Section
- settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '')
- urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Welcome.urls')),
path('auth/', include('Authentication.urls')),
path('ad/', include('Ads.urls')),
path('user/', include('UserDashboard.urls')),
path('admin/', include('AdminDashboard.urls')),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Apps Section
- template
<link href="{% static 'css/user/style.css' %}" rel="stylesheet">
- Folder Structure
You get this error because the Django project searches your static folders in the wrong place.
This will work:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I have been trying to display my images in my html page and I did do it correct but it wont work I don't know why this is the last update of django I have watched and read alot of videos and article and do the same as they told me to do but it not working the image wont work in my html template
Hope anyone can help me with this
Thank you
admin.py
from django.contrib import admin
# Register your models here.
from .models import *
admin.site.register(slideshowgo)
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
MEDIA_ROOT = BASE_DIR / 'static/images'
models.py
from django.db import models
class slideshowgo(models.Model):
image_one = models.ImageField(null=True,blank=True)
slideshow html
<div class="slide">
<img class="mySlides imgslide" src="{{slideshowgo.image_one.url}}">
</div>
urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('card_store.url')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In settings.py put the following config
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
And in your main urls.py file ( one which is located in the same app as of settings.py )
Add this
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will work for DEBUG = True
For DEBUG = False it won't as Django cannot server media files by itself, so you need to setup NGINX or APACHE.
One workaround is to put the following URL's in your main urls.py ( Not recommended for production )
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
I am trying to configure static files for django v2.2.1 following this https://docs.djangoproject.com/en/2.2/howto/static-files/
In settings.py:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In urls.py:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Project directory:
But I'm getting this error when running localhost:8000
I've been scratching my head for too long.
What am I missing here?
If you want the picture as your response you will have to visit http://127.0.0.1:8000/media/no_picture.png
If you want access to the admin page visit http://127.0.0.1:8000/admin/
You aren't receiving any response on http://127.0.0.1:8000/ because you haven't defined a view for that url pattern.
To write a Hello World view for example check out this tutorial from django documentation.
I'm following the official documentation in order to serve static files but I'm recieving error 404 in the development console. I'm using 'django.contrib.staticfiles', so static files should be automatically served. This is my setup:
Settings:
STATIC_ROOT = ''
STATIC_URL = '/static/'
Template header:
{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/app.css" %}">
Directory tree:
django_project
\apps
\static
\css
app.css
\templates
index.html
I can see in firefox console that the path to my file is correct:
So the problem must be that Django is not serving static files. I can't find what I'm missing. Any advice is more than welcome.
SOLUTION:
I was missing this line in my settings.py
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static'),)
It looks it's mandatory, same as TEMPLATE_DIRS.
When you run collectstatic it puts all your static content in the path specified by STATIC_ROOT. Check the deploy to production docs
If you are using the django server try checking the path that is being generated by {% static %}...you may have some trailing slash or something missing.
Check that you have are following all the requirements. You need to have django.contrib.staticfiles in your installed apps and something like this in your main urls file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
That should work :)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'project', "static"),
)
example of context_processors from settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
'django.core.context_processors.request',
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
)
example of installed apps in settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
urls.py:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
For anyone running django-cms and experiencing 404 errors (particularly, all of your static files are having "en-us" prepended to the URL), I found the following steps to help.
First, turn off internationalization of pattern matching in your urls.py file, as described here:
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
should instead be:
from django.conf.urls import patterns
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
The import is important, because the configuration of django-cms removes the patterns import from django.conf.urls.
This solved the redirect, but it still wasn't finding my static files. I needed to manually add the static url to the url patterns, like this:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
After that, static files worked as expected.
I'm sure that this is probably related to me messing up my configuration as a complete novice to Django. But since others might have the same problems, I'm putting it out there as a possible, if less than ideal, solution.
I somehow got the application running on django (new to python and django) and although the page loads the default URL (127.0.0.1:8000) but it does load the css files. It produces following error css files are directly accessed.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/css/bootstrap.min.css
'css\bootstrap.min.css' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Here is my settings.py page:
STATIC_URL = '/static/'
# Template location
TEMPLATE_DIRS = {
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),
}
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "media")
STATICFLIES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static")
)
and Here is the urls.py page:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'signups.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
can someone help with this please?
After you add any kind of static files to your project you should execute
python manage.py collectstatic