I'm using, in a Django 1.3 project, a CDN for media resources like images, css and such. I have a problem serving admin resources, what I made is:
In settings.py:
MEDIA_URL = 'http://cdn.test.com/'
ADMIN_MEDIA_PREFIX = '/admin_media/'
In INSTALLED_APPS:
'django.contrib.staticfiles',
And in urls.py:
(r'^admin_media/(.*)', 'django.views.static.serve',
{'document_root' : 'django/contrib/admin/media/',
'show_indexes' : True}),
Looking at the admin HTML I see something like /admin_media/css/base.css but all resources return http 404. I can't understand what is wrong.
Many thanks.
Try the following:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
# (... other urls ...)
)
urlpatterns += staticfiles_urlpatterns()
Also, the document_root you refer to in your urls.py should be an absolute path: this might also cause some issues. The above should replace this for your Django version, though.
Related
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)
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
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)
I know this question appeared like a milion times, also I'm not a newbie django coder still recently I'm having terrible problems gotting static files work in django projects. Last time I worked around this by using 1.3 but now I'm working on 1.2.5 version and none of the solutions works.
So basically I have statics worknig only in admin section and nowhere else around the site.
Static files are in K:/project/media, I'm using window, python 2.7 and django 1.2.5 and the development server.
Settings :
MEDIA_ROOT = path.join(path.abspath(path.dirname(__file__)), 'media')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/admin_media/'
URLconf :
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'K:/project/media/', 'show_indexes': True}
),
)
Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?
EDIT
Full URL conf
from django.conf.urls.defaults import *
from django.views.generic.simple import redirect_to
from p.globals import views
from p.newsevents.feeds import ReleaseFeed
import filebrowser
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
feeds = {
'releases': ReleaseFeed,
}
urlpatterns = patterns('',
(r'(?P<reqPath>[\w\-,.]+)$', redirect_to, {'url': '%(reqPath)s/'}),
(r'^admin/filebrowser/', include('filebrowser.urls')),
#(r'^admin$', redirect_to, {'url': '/admin/'}),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
#(r'^about-us/$', redirect_to, {'url': '/about-us/our-story/'}),
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),
(r'^get-your-look/', include('p.getyourlook.urls')),
(r'^about-us/', include('p.aboutus.urls')),
(r'^our-services/', include('p.services.urls')),
(r'^news-events/', include('p.newsevents.urls')),
(r'^promotions/', include('p.promotions.urls')),
(r'^careers/', include('p.careers.urls')),
(r'^locations/', include('p.locations.urls')),
(r'^$', views.home),
(r'^(?P<reqPath>[\w\-,.]+)/', views.globals_views),
)
from django.conf import settings
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'K:/Kuba/Webdesign/hair/media/', 'show_indexes': True}
),
)
I guess it`s because of this:
(r'^(?P<reqPath>[\w\-,.]+)/', views.globals_views),
It will intercept all request to static, and will call that view, which will cause TemplateDoesNotExist error.
Put a static url before this or change this( and in urls that it refers to) regex.