Django 3.0.7 unable to change STATIC_ROOT path - python

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"),
]

Related

Django Media Files Not Showing

My Django files are loading after upload and are shown in the media folder, however I cannot access them at localhost:<PORT>/media/<FILENAME._EXT>. I've looked at several other answers on stackoverflow and they haven't worked. For example adding the urlpatterns += static(...), having DEBUG=True in settings.py.
When accessing: http://localhost:8000/media/controller.gif:
Error:
lightchan-backend-1 | Not Found: /media/controller.gif
lightchan-backend-1 | 2022-03-06 16:37:34,875 WARNING Not Found: /media/controller.gif
In settings.py:
DEBUG = True
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
In my urls.py:
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('comment/<int:comment_id>/', views.comment, name='comment'),
path('comments/', views.comments, name='comments'),
path('reply/<int:incoming_id>/', views.reply, name='reply')
]
# if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

CSS is not loading in Django Admin Panel App? [duplicate]

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)

I started a Django project with all requirements but static files can't load successfully

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)

Django CKEditor Image Uploads not appearing

This is a duplicate of Django Ckeditor image browser not finding images, but I believe the answer there is wrong (there is an obvious bug in it with an undefined variable, not to mention the lack of Python indentation).
I'm using Django CKEditor 5.0.3 and Django 1.9.6. I am able to upload images in my admin, but they appear as a red X within the admin and do not appear on my site.
I'm still struggling a bit with MEDIA_ROOT and whatnot, but I think I have it right:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = "image_upload/"
MEDIA_ROOT = os.path.join(BASE_DIR, "image_upload")
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_UPLOAD_SLUGIFY_FILENAME = False
My urls.py, including my attempt at cleaning up the linked answer:
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from mainsite.views import HomepageView, AboutView, ContactView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^admin/', admin.site.urls, name="admin"),
url(r'^$', HomepageView.as_view(), name="homepage"),
url(r'^about/', AboutView.as_view(), name="about"),
url(r'^contact/', ContactView.as_view(), name="contact"),
url(r'^blog/', include("blog.urls", namespace="blog")),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$',
'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}
),
]
urlpatterns += staticfiles_urlpatterns()
Using CKEDITOR_UPLOAD_PATH = 'uploads/' makes django-ckeditor to upload an image to /media/uploads/, like:
settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
CKEDITOR_UPLOAD_PATH = 'uploads/'
When using the Django's dev server, static files are served by default but not media files, so you can force the server to consider them, the url configuration below should work.
urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.views.static import serve
from .views import HomeView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', HomeView.as_view(), name='home'),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
# serving media files only on debug mode
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
The missing function patterns from the old example was an old function I believe used on Django 1.6 or 1.7.
After installing ckeditor, perform the following :
In Settings.py:
add 'ckeditor' and 'ckeditor_uploader' into INSTALLED_APPS.
Add CKEDITOR_UPLOAD_PATH = 'uploads_directory/'
(Do not join MEDIA_ROOT with the upload_directory, ckeditor will take the MEDIA_ROOT as its root upload directory)
In your models files:
USE : from ckeditor_uploader import RichTextUploadingField and modify your required model field to type RichTextUploadingField
In urls.py:
add re_path(r'^ckeditor/', include('ckeditor_uploader.urls')) into urlpatterns
Using Django 1.8 with django-ckeditor 5.3.0, I was getting the exact same symptoms as those above (uploading files worked, but the src attribute of the <img> tag was set incorrectly, causing a red "X" in the preview and broken image links upon publication).
In my case, however, I didn't have to change anything in urls.py. My problem was that I had:
CKEDITOR_UPLOAD_PATH = os.path.join(MEDIA_ROOT, "ckeditor")
So my mistake was giving CKEDITOR_UPLOAD_PATH the path where I wanted ckeditor to upload to (logical, no?).
The fix was to change the above line to
CKEDITOR_UPLOAD_PATH = "ckeditor"
In hindsight I can see how this allows django-ckeditor the ability to use the MEDIA_ROOT for uploading and the MEDIA_URL for serving. Still I thought someone should say it: "Don't use the full path when setting CKEDITOR_UPLOAD_PATH!"
I hope this saves others some time.
For Django 4 the steps to enable image or file upload in django-ckeditor is:
1. Install django-ckeditor
pip install django-ckeditor
2. Update settings.py
Add file upload path:
CKEDITOR_UPLOAD_PATH = "uploads/"
Add ckeditor,ckeditor_uploader in INSTALLED_APPS:
INSTALLED_APPS = [
...
# plugins
'ckeditor',
'ckeditor_uploader'
]
3. Update urls.py
Add path('ckeditor/', include('ckeditor_uploader.urls')) in urlpatterns:
urlpatterns = [
...
path('ckeditor/', include('ckeditor_uploader.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
4. Use RichTextUploadingField in models
from ckeditor_uploader.fields import RichTextUploadingField
class ResearchTopic(models.Model):
title = models.CharField(max_length=200)
description = RichTextUploadingField()
Tested with:
Django==4.0.4
django-ckeditor==6.4.0
References:
django-ckeditor documentation
The #Mohammed-tayab's solution worked for me with a little modification:
from ckeditor_uploader.fields import RichTextUploadingField

Django file-browser and error 404

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)

Categories

Resources