Django trouble serving media files - django

Getting back into django after a hiatus of a few months. Using 1.3. I have this url pattern:
(r'^(?P<path>[a-zA-Z0-9]{4}.jpg)','django.views.static.serve',{'document_root':'/media/uploads'})
I put test.jpg in /media/uploads. I go to localhost:8000/test.jpg and the url seems to match the pattern, but I get a 404 - ""/media/uploads/test.jpg" does not exist". What could the problem be? I saw a thread where someone was having the same problem and the reason ended up being that MEDIA_URL and ADMIN_MEDIA_PREFIX were the same, but that's not the case here.

document_root should be an absolute path.
Unless you literally have a file at /media/uploads/test.jpg
It's probably more like /path-to-my-project/media/uploads/

Related

How to serve a text file at root with Django

I got a site, say it's "www.site.com". I need to serve a text file at the root, so that "www.site.com/text.txt" will show up.
I read through this answer: Django download a file, and I used the "download" function.
However I am at a loss how to configure the URL. Can someone help me out?
Say I put this this into my url.py,
url('^(?P<path>.*)/$', san.views.FileDownload.as_view()),
This then supersedes all my other url patterns and render them useless. How do I make this work? Thanks!
Put it as the last urlpattern in your urls.py to ensure it doesn't sccop up everything. It should not have the trailing / either, i.e.
url('^(?P<path>.*)/?$', san.views.FileDownload.as_view()),
This will match the request "/YOUR_FILE.txt", and is also case-insensitive.
url(r'^(?i)YOUR_FILE.txt$', san.views.FileDownload.as_view()),

Django-tinymce + django-filebrowser-no-grapelli doesn'work

I am confused a bit. I have tried different ways and manuals, but nothing works for me.
What I use: django 1.5, django-page-cms, django-tinymce. I need filebrowser for tinymce for using in admin. I stopped at https://github.com/wardi/django-filebrowser-no-grappelli , becouse it looks simpliest in usage and i don't need Grapeeli, which needs for https://github.com/sehmaschine/django-filebrowser. So I need:
add 'filebrowser' in INSTALLED_APPS;
add (r'^admin/filebrowser/', include('filebrowser.urls')), to urls before admin
change /templates/admin/index.html adding {% include 'filebrowser/append.html' %} before {% for app in app_list %}
After doing those steps and correcting append.html (for django 1.5 - change url look), I receive error Reverse for 'tinymce-filebrowser' with arguments '()' and keyword arguments '{}' not found. when I want enter into the page, where tinymce (and filebrowser) are in usage. Google can't get me certain answer what I need to do.
So I am hope for your help. If anyone has worked with such libraries, can you tell me please, What I need to do in that case? Or is there another working filebrowser exists with the understanding configuration?
UPDATE. After adding tinymce urls and editing almoust all filebrowser templates (becouse of django 1.5). I have some problems:
at the page with tinymce redactor I see in console error: localhost:8000/admin/tinymce/filebrowser/ 404 (NOT FOUND) . tinynce still works fine, but i can't use filebrowser's image-link, which appears after adding filebrowser (error in console: Cannot call method 'apply' of undefined )
uploading of files (directly, without tinymce) always fails here: localhost:8000/admin/filebrowser/upload_file/ 500 (INTERNAL SERVER ERROR)
Any ideas how to solve those problems?
UPDATE. For solving first problem you need put (r'^admin/tinymce/', include('tinymce.urls')), before url(r'^admin/', include(admin.site.urls)),.
For solving second problem you need use right package of django-filebrowser-no-grapelli, preferably where no flash_login_decorator for upload function.
It seems that you have not added django-tinymce urls in urls.py:
(r'^admin/tinymce/', include('tinymce.urls')),

What does "Directory indexes are not allowed here." mean in a Django error?

I am trying to debug this bizarre 404 error that surfaced in my Django application.
Page not found (404)
Request Method: GET
Request URL: http://78.198.124.245/
Directory indexes are not allowed here.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
What does "Directory indexes are not allowed here." mean? What is a directory index?
I Googled around, and the results I found all have to do with serving static files. However, I don't think I do that. What does this error mean?
Check your settings.py file for the STATIC_URL value. You want the value to be the subfolder where your static files are stored - generally STATIC_URL = '/static/'.
The answer depends on which django version you are using. For 1.4+ then just set the STATIC_URL
For 1.3.x its not so much what the STATIC_URL is set to as what your ADMIN_MEDIA_PREFIX is set to.
If you set it to /admin/ then django development server will attempt to serve static files for everything under /admin/ out of the contrib/admin/media/ folder
This means that http://127.0.0.0:8000/admin/postz/post/473 will attempt to find static content at django/contrib/admin/media/postz/post/473 and that's what the 404 is
If you are trying to access http://127.0.0.0:8000/admin/ then that would be an index.html inside of the admin media directory but the internal static server does not allow indexes so that's the error that it throws.
The accepted answer isn't exactly correct. Setting STATIC_URL may have worked as a side effect, but the real issue was that ADMIN_MEDIA_PREFIX was wrong.
The best settings would be:
ADMIN_MEDIA_PREFIX = '/media/'
or
ADMIN_MEDIA_PREFIX = '/admin/media/'
For 1.4 then just set the STATIC_URL as ADMIN_MEDIA_PREFIX is deprecated
https://docs.djangoproject.com/en/dev/releases/1.4/#django-contrib-admin
I had the same problem sometime ago and I also was looking for an answer but couldn't find anything useful. However, the problem for me was the the name I entered in the path in the urls file in my app didn't match the name of the function in views.
path('home-en', views.home_en, name="home")
Here 'home-en' should've been 'home_en' to match the name of the view function.
The reason for getting this error is that maybe trying to map our view with a custom url which luckily is a directory name.
so in this case, for e.g: we're storing our all static files in Static named folder and we're trying to create a url : path('static/',views.static_view).
So in this case it will not work.
what we can do, we can change the custom url to any generic url such as 'inex/' and it will work.

Serving static files in development with django-devserver

I finally got around to trying out django-devserver. It's installed and working but static files refuse to load (Django 1.3). (Note: static files work fine with the standard runserver management command, before switching to django-devserver, so all the configuration settings are fine.) I came across this bit in the README:
DEVSERVER_IGNORED_PREFIXES = ['/media', '/uploads']
A list of prefixes
to surpress and skip process on. By default, ADMIN_MEDIA_PREFIX,
MEDIA_URL and STATIC_URL (for Django >= 1.3) will be ignored (assuming
MEDIA_URL and STATIC_URL is relative)
Which seems very odd because the whole point of using runserver is to not have to have an actual real web server setup, especially just to serve static files in development.
Oddly, though, even though it mentions ADMIN_MEDIA_PREFIX, I found that the admin actually loads all its static resources fine, which leads me to believe that maybe I'm just missing something somewhere.
Anyone ideas?
From the URL in #MarkLavin's comment, I actually came across (rather, reminded of) the following:
# Add to end of urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
That will allow the static files to be served and is ignored in production, so there doesn't seem to be any side effects to the approach. However, it irks me a little to have to modify my urls.py just for this, but it's probably only temporary until the noted pull request is merged.
If anyone has any other solutions, feel free to add them, though.

No Style/css On Django Admin

I'm a complete newbie to apache, so the the whole deployment process has been a pain in the backside, so bear with me.
I'm having the problem of my admin site using the css formatting, it's just coming up as plain html.
I know there are other questions that answer this issue here and elsewhere, but for the life of me I can't figure out to get it working. So if anyone can spot what I'm messing up I'd be eternally grateful.
The relevant lines in settings.py are:
STATIC_ROOT = '/var/www/logparseradmin/logparser/static'
STATIC_URL = 'http://theyard/logparseradmin/static/'
ADMIN_MEDIA_PREFIX = '/var/www/Django-1.3.1/django/contrib/admin/media/'
and I have:
Alias /static/ /var/www/Django-1.3.1/django/contrib/admin/media/
in my httpd.conf.
I've tried a whole bunch of variations on this, as the various answers on the Internet have suggested, but no luck.
Thanks very much.
There's at least three things wrong here.
Firstly, your STATIC_URL is not probably a valid URL - http://theyard/ is not a real domain name, and won't work unless you've got local DNS resolution (which, given your self-described newbie status, seems unlikely.)
Secondly, the path value of STATIC_URL doesn't match the Alias you've put in httpd.conf - STATIC_URL has /logparseradmin/static/, whereas Alias just has /static/
Thirdly, ADMIN_MEDIA_PREFIX should be a URL path, not a file path.
And without seeing the rest of your http.conf it's impossible to be sure, but there may be fourth issue with mod_wsgi matching the URL before your Alias is processed.