So i pushed my Code to Heroku and I have a lot of trouble getting things to work. The Static files do not work, I installed WhiteNoise and so on but I break the site as soon as I use {%static 'js/whatever.js'%} but {% load staticfiles %} can be used without breaking the server. Currently my theory is that the admin won't load because of the static files but they are not required for the normal django admin right?
Here are my important lines I hope somebody finds the mistake.
settings.py
DEBUG = False
ALLOWED_HOSTS = [u'....herokuapp.com']
import dj_database_url
DATABASES ={'default':dj_database_url.parse('postgres://...jl4',conn_max_age=600)}
INSTALLED_APPS = [
'rest_framework',"rest_framework.authtoken",'django.contrib.admin','django.contrib.auth','django.contrib.staticfiles',]
AUTHENTICATION_BACKENDS =('django.contrib.auth.backends.ModelBackend',)
SITE_ID = 1
WSGI_APPLICATION = 'ABC.wsgi.application'
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static'), ]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATICFILES_STORAGE ='whitenoise.django.GzipManifestStaticFilesStorage'
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ABC.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
application.add_files('/ABC/static/', prefix='/static/')
Procfile file, requirements.txt, .git folder are present and look normal. I have no problems on 127.0.0.1 at all. No migration problems anywhere. Heroku is configured with a Python Buildpack and Postgres. 404 500 etc Handlers are installed and working (Btw I get a 500 Internal Server Error).
When I push the Code online with DEBUG=True (I know you should not do that but im totally frustrated), I have no Problems at all. Can Access the Admin and can Load the static files without any Problems.
So If anybody has an Idea whats missing please tell me. There are no stupid comments/questions I need answers!
from django.contrib import admin
from django.conf.urls import (
handler400, handler403, handler404, handler500
)
handler400 = 'my_profile.views.server_error'
handler403 = 'my_profile.views.server_error2'
handler404 = 'my_profile.views.server_error3'
handler500 = 'my_profile.views.server_error4'
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^my_profile/',include('my_profile.urls',namespace='my_profile')),
url(r'^login/',include('login.urls')),
url(r'^logout/',include('login.urls')),
url(r'^register/', include('registration.backends.simple.urls')),
url(r'^userprofile/',include('userprofile.urls')),
url(r'^about/',include('about.urls')),
url('^inbox/notifications/',include(notifications.urls,namespace='notifications')),
Related
This question already has answers here:
Why does my Django admin site not have styles / CSS loading?
(22 answers)
Closed 1 year ago.
I have created my first Django project, but in the admin panel, when I run http://127.0.0.1:8000/admin/ the CSS files is not loaded and I also created a new Django app but still got the same error, I have also visited the question but my problem is not solved: Django admin site not showing CSS style
It looks like this:
I can log in:
It should look like this:
settings.py
import os
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
Note: I have used STATIC_ROOT = os.path.join(BASE_DIR, 'static') in settings.py and also run the command python manage.py collectstatic, but still, I got the same thing.
Assuming you have DEBUG=False in settings.py, you need to manually serve static files. Note that this is not recommended for production - you should serve static files using your web server such as apache on nginx. Also note that django will do this automatically, if you have DEBUG=True.
urls.py:
import re
from django.urls import re_path
from django.conf import settings
from django.views.static import serve
urlpatterns = [
...
re_path(r'^%s(?P<path>.*)$' % re.escape(settings.STATIC_URL.lstrip('/')), serve, {"document_root": settings.STATIC_ROOT}),
]
try this might help
from django.conf.urls import url
from django.views.static import serve
from django.conf.urls.static import static
from django.conf import settings
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
or if it still does not work for you
set debug=false
thane python manage.py collectstatic after creating a static cdn folder and paste you localhost path in allowed host section and then tell us if it still not loading admin css
Thanks, Guys I added the following in my setting.py file, and now it's working fine:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
As I state in the title, when I set DEBUG = False in my project's settings file, the files from my media directory (the one the user uploads) don't display. The files from my static directory (the CSS and JavaScript files) load properly.
I looked at this answer, but I don't understand the prerequisites to get this to work. I am testing this on my local machine, where I only have Django and PostgreSQL installed. I don't have any Apache servers running, as far as I'm aware. I want to deploy my app on Amazon AWS, so I'd like to try out how it will look in production there before I deploy it to Amazon AWS.
Here are the relevant parts of my settings.py file:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
STATICFILES_DIRS = [
BASE_DIR / "static",
'/var/www/static/',
]
Willem Van Onsem is right on the media files. Since the static file is OK, for the workaround, you can add this line to your urls.py in the folder containing settings.py:
from django.conf.urls.static import static
from django.conf import settings
urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I'm using Django version 1.10. Project work fine on Debug = True, but when I set it to False is not. Django just can't find static files.
My Django settings looks like:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'master',
'update',
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
And the urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^master/', include('master.urls')),
url(r'^update/', include('update.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
uwsgi.ini file
[uwsgi]
chdir = %v
virtualenv = %v/py
module = go_conf.wsgi
master = true
http = :8000
vacuum = true
buffer_size = 64k
max-requests = 100
daemonize = %v/log.txt
I aslo used python manage.py collectstatic, and it collected everything but still not working.
I tried to solve this by reading other articles on this site, but nothing really worked for me.
Hope, that someone will at last help.
This is the Django desing. A quote from the docs for Static file development view:
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.
If you are setting DEBUG=False you are probably going to make it production. If so, your static files must be served by a webserver (eg. Nginx, Apache etc).
check the whitenoice library,
it works great for development and production environment
Radically simplified static file serving for Python web apps
I have Django Grappelli == 2.6.5 with Filebrowser and Tinymce. I use Django ==1.7.5.
I have problem with uploading images. Image uploads in the browser, appears in the folder \media\uploads but Filebrowser doesn't see it and throws an error: 'media\uploads\img.jpg' could not be found
Here is my settings:
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))
DIRECTORY = getattr(settings, "FILEBROWSER_DIRECTORY", 'uploads/')
In the pyhon shell I did following:
from django.conf import settings
import filebrowser.settings
filebrowser.settings.MEDIA_ROOT
C:\\mysite\\static\\media
filebrowser.settings.DIRECTORY
uploads/
Why is this happened? How can I set the right upload directory to Django-Filebrowser? I've read documentation and searched for answears in google. But I don't understand how fix it and I'm confused. Can you help me please.
in your projects urls.py:
from django import views
....
urlpatterns = [
...
url(r'^media/(?P<path>.*)$', views.static.serve, {
'document_root': settings.MEDIA_ROOT})
...
]
So I installed Bitnami Django stack, and enabled the admin module, and followed the tutorial for creating an admin menu for "Polls".
However, when I go to /admin/ everything is white plaintext. All the css and images are 404 error.
All I did was:
enable in settings.py installed_apps:
'django.contrib.admin',
In urls.py UNcommented:
from django.contrib import admin
admin.autodiscover()
url(r'^admin/', include(admin.site.urls)),
uncommented.
In settings.py, I tried using default settings and also tried this:
MEDIA_ROOT = ''
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)
Nothing seems to work, it refuses to find static files in /admin/media/css/ etc.
I made sure my windows PATH has /bin of django. I even tried including /contrib, nothing helps.
I have installed Django to:
C:\DjangoStack\apps\django
I have installed my project to:
C:\Users\dexter\BitNami DjangoStack projects\Alpha
and I type: localhost/Alpha/admin to go to admin.
I almost missed the answer to this until I re-read your question and finally caught the bit in the last line: "and I type: localhost/Alpha/admin to go to admin". That means all your URL settings are wrong.
Currently, you have:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
Whereas, these should be:
MEDIA_URL = '/Alpha/media/'
STATIC_URL = '/Alpha/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
Additionally, you don't need "static/" in STATICFILES_DIRS. So remove that setting.
I'm from the BitNami Team and I just saw this issue. I'm not sure if you are using and older version but at least in the newer version of the BitNami DjangoStack you just need to make sure that the ADMIN_MEDIA_PREFIX point to /static/admin/ if you are following the
instructions in https://docs.djangoproject.com/en/1.3/intro/tutorial02/ . You don't need to copy the static files in your project directory because django automatically will use the files in django/contrib directory.
However currently we are setting the ADMIN_MEDIA_PREFIX to /static/admin/media because the behavior is different when the application is served by Apache instead of the django server. We realize that this may be a bit confusing for users that are just starting with django and we are looking at this on our side to keep the default configuration for the new projects but also allow the demo project to be served by Apache.