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)
Related
I'm working with Python 3.8.3, pip 20.1.1, Django 3.0.6 and virtual environment are already installed but I'm not getting the style from my CSS files and
the web page behavior from JavaScript onto my web page and here is the photo of errors in my console
In my settings I configured well all about the static file and the template and the following is my
settings.py
STATICFILES = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
and that's also the the URLs for the whole projects
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('estate_web.urls'))
]
and I done well all about the URL configuration and the following is my app URL
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home')
]
and that's the views which return the page
views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'index.html')
I don't know if there is any error in my codes but I can't get the website as I expected and I want to know if there is any fixation of this because I met with it couple times.
if you do it for your debug server, you can add this lines to your urls.py
from django.conf.urls.static import static
...
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Hi I am new to django,
I have created a django project with following urls.py and settings.py codes respectively
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("user_mgmt.urls")),
path('',include("dashboard.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"spaat_static")
then I am collecting the static files using:
python manage.py collectstatic
but Django is collecting files in the ../staticfiles directory, whereas I have defined ../spaat_static in STATIC_ROOT.
How to solve this issue ?
Thank you in advance!
THIS LINE :
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"spaat_static")
Try to change it to :
STATIC_ROOT = os.path.join(BASE_DIR,"spaat_static")
And tell me how it goes !
Peace.
Don't forget to add this :)
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"spaat_static"),
]
I can upload an image through the admin page, but the image can not be found when I navigate to the url that is generated by django. (404 error)
The files are being uploaded to the folder:
project_root/media/eventbanner/1/
I have tried multiple solutions but none seem to work for my situation.
Django 1.10 is being run local on Ubuntu 16.04.
The url I get is:
http://localhost:8000/media/eventbanner/1/banner_image.jpg
Media root folder is located at:
/home/username/xxx/xxx/project_name/media
Code in HTML file:
<div class="banner-image">
<img src="{{ event.eventbanner.banner_image.url }}"/>
</div>
url.py code:
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'events'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^details/(?P<event_id>[0-9]+)/$', views.details, name='details'),
url(r'^details/(?P<event_id>[0-9]+)/addcomment/$', views.add_comment, name='add_comment'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static'),]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
models.py
def validate_only_one_instance(obj):
model = obj.__class__
if (model.objects.count() > 0 and obj.id != model.objects.get().id):
raise ValidationError("Can only create 1 %s instance" % model.__name__)
class EventBanner(models.Model):
event = models.OneToOneField(Event, unique=True)
banner_image = models.ImageField(upload_to=get_image_path, blank=True, null=True)
def clean(self):
validate_only_one_instance(self)
The real problem here is that there is no relationship between this url http://localhost:8000/media/eventbanner/1/banner_image.jpg and this location on disk /home/username/xxx/xxx/project_name/media.
In a production application you'd have a web server where you'd store your Media content, the serving URL would be MEDIA_ROOT and you'd append ImageField.url to this value to get a valid image path.
What you need here is to set up a web server for your media images. At first that sounds like a lot of work, but Django provides a shortcut...
Serving Files in Development
You have some work you need to do to have the media files served locally. It requires some changes to your urls.py ...
from django.conf import settings
from django.views.static import serve
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]
This uses the views.serve bit and should only be used in DEBUG mode. It overrides the path to media files(django's term for user uploaded content like ImageField). This will redirect those requests through the serve view. Best I can tell this is a mini web server that will map those request routes to locations on disk and allow those locations to be reachable via HTTP urls.
As of at least Django 1.8, there is a helper function static() that will set this up for you and ensure that it only functions in debug mode.
Your urls.py should look something like this:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
...to simply quote the documentation.
Make sure that your MEDIA_URL is set to a relative path like /media/ and that your MEDIA_ROOT is an absolute filesystem path like /home/foo/project/media.
I'm using django admin site (v1.9.6) and recently installed file-browser for managing images and documents. I followed installation steps and file-browser is shown in admin interface but I cant upload images and documents. When trying to access them 404 errors shows up (also thumbnails are not shown in file-browser page):
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/uploads/aaaaaaaaaaalula.png
But the file is there, if I make
ls media/uploads/aaaaaaaaaaalula.png
it shows the file, so it's uploaded.
my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
and url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from filebrowser.sites import site
urlpatterns = [
url(r'^admin/filebrowser', include(site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include(admin.site.urls)),
]
Thanks in advance for help.
C.
Ok, here is the solution:
https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
The issue was that I was testing django-filebrowser with development server
You are not putting url for displaying image.
In setting.py-
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '/media/'
In URLs.py--
Url(r'^media/(?^<path>.*)$','django.views.static.serve',{'document_root':
settings.MEDIA_ROOT,}),
I think it can help you
You can do:
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#your urls
]
if settings.DEBUG: # will be 'True' in development
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
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)