django two 'static' path in my url? - django

My static file setting is
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
and i install ckeditor in my project, and in urls.py :
url(r'static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT,}),
(r'^ckeditor/', include('ckeditor.urls')),
when i get ckeditor.js from http://127.0.0.1:8000/admin/chicinfo/article/add/static/static/ckeditor/ckeditor/ckeditor.js, I can't get this file.
Below picture show my problem:
What happen to me?

I suspect you are putting /static/ in the actual url in your templates. E.g,
<scirpt src="{{ STATIC_URL }}static/ckeditor/ckeditor/jkeditor.js" />
You don't need to put the static bit in if you are using static_url. It should be:
<script src="{{ STATIC_URL }}ckeditor/ckeditor/jkeditor.js" />
You should also put the static files serving URL at the end of your url config.
Working example:
url(r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT, }),

Related

How to manage media files on Windows?

I want to get media files for Django when developing application. But they just don't set up. This is my settings:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR
STATICFILES_DIRS = (
STATIC_ROOT + '\\projectpackage\\static\\',
)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '\\projectpackage\\media\\'
Templatetag:
<img class="img-responsive" src="{{ project.image.url }}" alt="">
Urls:
urlpatterns = [
url(r'^profile/', include(profile.urls)),
url(r'', include(authentication.urls)),
url(r'^project/', include(project.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^', 'views.index', name='index'),
url(r'^markdown/', include('django_bootstrap_markdown.urls')),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
I can't see my mistake, because I've tried every other advices in other questions. And everything works bizzare. Static files are running good, media uploading to media/prj_img, but when I try to show image at template I've got such strange result:
<img class="img-responsive" src="/media/C%3A/Development/projectdirectory/projectpackage/media/prj_img/wallhaven-131_od9FWLX.jpg" alt="">
How could I fix this media error? This is strange, because everything looks right. Why there are full path in url?
Edit:
BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Also I've figured out that I've got wrong upload_to and changed it to prj_img. Now I've got following link:
<img class="img-responsive" src="/media/prj_img/wallhaven-24700.jpg" alt="">
But still it's not displating.
I have a lot of comments on your code.
STATIC_ROOT location is not appropriate. With your settings collectstatic command will put everything in your project's directory. Change it like so:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
But take care, that this folder is accessable (both permissions and django-settings) in production.
Use os.path.join to join folders:
STATICFILES_DIRS = (
os.path.join(STATIC_ROOT, 'projectpackage', 'static')
)
This url pattern must be the last one and contain $:
url(r'^$', views.index, name='index'), # do not pass strings
These two url patterns do the same (docs):
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
Moreover, you do not need them. Just ensure, that INSTALLED_APPS setting contains 'django.contrib.staticfiles'.

Why is django not serving my media files?

In my settings.py,
ROOT_PATH = os.path.dirname(__file__)
#STATICFILES_DIRS =
STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
MEDIA_ROOT = os.path.join(ROOT_PATH, 'media')
STATIC_URL = '/static/'
MEDIA_URL = '/media/
in my template,
<div class=" event_image">
<img src="{{ MEDIA_URL }}{{ restaurant.logo }}" />
</div>
This doesnot work in development, it returns a 404 "GET /media/restaurant_detail/restaurant_detail/information_about_object.jpg HTTP/1.1" 404 178672
what am i doing wrong,what is the right way to go about it so it works in both in dev't and production. i looked here (Django 1.4 serving MEDIA_URL and STATIC_URL files on development server) but all in vain.
Turns out i had to include the urls,my urls
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
The above worked..
my project structure
-myproject
-- media
-- static
-- templates
-- settings.py
-- manage.py
-- app
Suziemac, It worked for me too. Here is a link for original documentation: https://docs.djangoproject.com/en/1.10/ref/views/ .
When I was developing an app I have used + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT), however this did not work when I deployed an app.

Django: Migrating from MEDIA_URL to STATIC_URL

Since Django 1.3 the concept of STATIC_URL has been introduced to separate use media files from css and js files.
I have set my STATIC_ROOT = '/home/user/project/static/' and
STATIC_URL = '/static/'.
In my base.html, i have changed the path like this:
<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet">
And in url.py I have added the following two lines:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
My view renders the template with RequestCOntext:
return render(request, 'main_page.html', variables)
But in the development I still get 404 when running runserver.
[18/Aug/2012 17:12:04] "GET /static/jquery/jquery-1.8.0.min.js HTTP/1.1" 404 1682
What could I be missing?
settings.py
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'site_media')
MEDIA_URL = '/site_media/'
STATIC_URL = '/static/'
if DEBUG:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
else:
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
That should work :)

Static file management in Django1.4

I've been confused with static files in Django for days. I found one solution that worked fine. But it collapsed when I set DEBUG=False. So I build up a new project and do some tests to get a clearer look.
First I create a project with the default settings. Then I changed some lines of the setting file into:
STATIC_ROOT = '%s/site_media' % PROJECT_DIR
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(STATIC_ROOT, 'images'),
)
After that, I put 'hi.jpg' at 'project_dir/images/hi.jpg'. I call runserver and visit 'http://127.0.0.1:8000/static/images/hi.jpg'. It doesn't work. What's the problem?
Here's how it works: when DEBUG=True then Django serves the static files itself. When DEBUG=False then Django won't do that anymore and you'll need to configure your web server to do it (such as Apache).
Django has a mechanism for that in django.contrib.staticfiles (see Managing static files and The staticfiles app). It basically means that you need to run the collectstaticmanagement command which will search for all static files in /static/ directories in your Django project and it will put them in one directory (STATIC_ROOT). When that has been done, your web server can serve the static files from that directory.
If one or more static files can't be found after running collectstatic then that means you have configured something incorrectly.
settings.py
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'site_media')
MEDIA_URL = '/site_media/'
STATIC_URL = '/static/'
if DEBUG:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
else:
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
:)
Django: Migrating from MEDIA_URL to STATIC_URL

How to show an image in template uploaded from admin panel Django 1.3

I'm a newbie to django. I'm developing a project in django 1.3. Problem is I'm uploading a image from the admin panel
class About(models.Model):
image = models.ImageField(upload_to='about')
files = models.FileField(upload_to='about')
Here is my template tag
<img class="profile_pic" src="{{ about.image }}" />
My setting file is as below
MEDIA_ROOT = path("media/")
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = (
path('static/'),
)
I checked that image is uploaded to /media/about/image_name. Problem is it rendered in the template as "/about/imagename" but not shoing. When I manually go to that image url it showing a 404 error.
<img class="profile_pic" src="{{ about.image.url }}" />
UPDATE
Also in your urls.py:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT})
It worked!... After importing setting.MEDIA_ROOT in the urls.py file then I added a media rule so ITs working now..
BTW It also need
{{ about.image.url }}
as #zsquare(thanks) said earlier Here is the media rule
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}),