Debug set on False and not load styles and pictures - python

I have a problem with static css files.
I added to your application (in django 1.8) Page 404 - 404 page template and code in a file urls.py:
handler404 = 'django.views.defaults.page_not_found'
My STATIC_ROOT:
STATIC_ROOT = os.path.join(BASE_DIR, 'public_assets')
In settings.py file, I have the code:
Debug = False
ALLOWED_HOSTS = ['*',]
ALLOWED_HOSTS = ['localhost', '127.0.0.1',] - also doesn't work
After starting website, do not load styles and pictures.

Not sure this will work are not
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes':True}),
)
add above at the end of root urls.py
and run collectstatic

Related

How to serve static files in django 3.1

I'm use django 3.1 when I have the option DEBUG = True in my settings.py the static files are serve fine, but when I use DEBUG = False the static files are not serve I all ready try with this solution:
404 Static file not found - Django
In the answer like in de official docs of django use this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But still doesn´t works. Someone have any idea why still doesn't works.
You can add this
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = (
os.path.join(BASE_DIR, 'staticfiles')
)
MEDIA_URL = '/media/'
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media')
)
mkdir static
python manager.py collectstatic --noinput
python manager.py runserver
it works

Static fiels doesn't load in django 1.11

I'm using django1.11 to build a web app.I have add required settings for static files in settings.py and all of static files was working fine. But I was trying to add 404 custom templates during that suddenly my static files stop loading.
Here's my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'IGui', 'static'),
)
Here's my urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', home_url.index, name='home'),
url(r'^dockers/', views.DockerStuff.as_view(), name='docker-stuff'),
url(r'^user/', include(user_urls, namespace='users')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My directory tree:
-IGui
IGui
-static
--css
--js
Help me, please!
from django.shortcuts import render_to_response
from django.template import RequestContext
def handler404(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
def handler500(request):
response = render_to_response('500.html', {},
context_instance=RequestContext(request))
response.status_code = 500
return response
Add in you views.py the following two views, and just set up the templates 404.html and 500.html with what you want to display.
No custom code needs to be added to urls.py
Just turn Debug to True in settings.py will solve this issue.

Django Upgrade: Static Files Not Served

I have upgraded to Django 1.10 from 1.9.6. Below is my urls.py file which previously worked:
from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
from dwad import settings
urlpatterns = [
url(r'', include('meta.urls')),
url(r'^straightred/', include('straightred.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^chaining/', include('smart_selects.urls')),
url(r'^tinymce/', include('tinymce.urls')),
url(r'^accounts/', include('allauth.urls')),
]
# Get Django to serve media files in debug mode.
if settings.DEBUG:
urlpatterns += [url(r'^resources/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]
if not settings.DEBUG:
urlpatterns += [
url(r'^resources/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
]
When try to run the site I get:
view must be a callable or a list/tuple in the case of include().
I know the above error is due to the 'django.views.static.serve' being in strings. If I remove them from strings and link to the actual view i get the following error:
name 'django' is not defined
If I delete everything from "Get Django to serve media files in debug mode." and below the site loads but no static or media files are served. This obviously means no css is applied and no images are loaded.
If people can offer some advice on next steps that would be appreciated.
Some settings that may be useful:
# Static files
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/str8red.com/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_URL = '/resources/'
MEDIA_ROOT = 'media' if DEBUG else '/var/www/str8red.com/resources/'
The reason behind the error is that, Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home') in your URL patterns.
Try this,
from django.conf.urls.static import static
if not settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
#If you are in production (means running using nginx, or apache),
# you don't need this setting. Because, the media and static files
# are served by the nginx/apache, instead of Django.
#if settings.DEBUG:
#urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Django test server gives 404 to static files

In production, I'm using NGINX and set an alias for static files, so this is fine.
However, in test server mode, I get 404 for all of the static files. They are located in the base of the project and here are my relevant settings:
DEBUG = True
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
And here is my urls.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
urlpatterns = patterns(
'',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'home.views.home', name='home'),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When running the test server, I try to get http://127.0.0.1:8000/static/home/img/logo.png, which is located in $BASE_DIR/static/home/img/logo.png, but get 404.
'home/img/logo.png' could not be found
and in the test server log
[03/Feb/2015 17:49:08] "GET /static/home/img/logo.png HTTP/1.1" 404 1655
Any ideas?
STATIC_DIRS should be STATICFILES_DIRS

static files error - Django - cmd error P/1.1 404 1712

Sorry...this is a long question...
I am learning django plus I am also new to python...so expect me to stupid as of now. I am running python 3.3 and Django 1.6.2.
I am working on setting urls via static files. I have also run collectstatic. I was told some of these commands may be obsolete for python 3.3, (I posted a comment on youtube and they said go back to python 2.7
I am following an online tutorial which is doing this on python 2.7 but I want to do it on python3.3(I guess I like making things difficult for me). when I run the code on the machine through cmd I get P/1.1 404 1712 error.
(https://www.youtube.com/watchv=8t80DMAAps8&list=PLEsfXFp6DpzRgedo9IzmcpXYoSeDg29Tx&index=7)
Even though I am stuck I would like to mention these tutorials are helpful...thanks to the uploader.
back to work...the error I receive says module not found and the exception location direct me urls.py line which 22...which is:
document_root=settings.STATIC_ROOT)
this is a part of:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I did play around with it and found that if I comment the settings.DEBUG code block the file works(but doesn't load css files) so I tweaked the path on settings.py but the debug still says url not found while accessing css path. I tried changing the url manually on the browser to the exact location of the file but still....nothing ( :'( I thought I was smart... but alas!).
Let me know if you have any other questions and THANK YOU!! :)
Also, this question might get side-tracked since there is a bigger(at least for me :(...) problem to solve...but can someone suggest any book or a video tutorial for a beginner to get very good at MVC and django in python?....Thanks :)
This is how my settings.py looks:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/whattheheck/static/'
# Template location
TEMPLATE_DIRS = {
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),
}
if DEBUG:
MEDIA_URL = '/whattheheck/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 this is how urls.py is:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls 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)

Categories

Resources