I'm having an issue with some administrative icons being used in a Django CMS I have deployed for one of my clients. As you can see from the attached image, the path to the static folder where the images are stored is wrong and therefore doesn't render the icons. It's missing the static part of the URL. Can anyone provide any pointers on this?
Many thanks in advance for any assistance people may be able to provide on this
In production go to,
/etc/nginx/sites-available/django
Remove or comment these lines
#location /static/admin {
# alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
#}
You can have a look on my website for this.
Related
Django currently has a complete system for routing urls.
But I have a very specific situation where I am using django but actually need to use urls like in classic PHP language.
For example:
The url - localhost/reader/theeffort should take me to a folder called theeffort where I have my files index.html, 1.html, 2.html, 3.html
and so on!
Now all these files should be accessible by localhost/reader/theeffort/*.html and not by Django's default url system. Is this possible to achieve that? If yes, how?
This isn't a thing you would do with Django's URLs. If you just want to serve HTML files within a folder, they are static files; they should therefore be served by the web server itself, eg Apache. You just need to configure an alias in the Apache conf to point to the folder where the static files are.
I'm starting to program in Django and need some advice from you.
My project is a catalog containing more than 1000 products. Each product has an image.
I need to optimize the loading time and management of these images and my question is: Where do I store these images? In static folder, in the media folder or another solution?
I read the Django documentation and searched on google on static files and media files, but only found recommendations on "store user uploaded data such profile image in Media folder". But I do not know if this recommendation would apply to my project to optimize loading time and management of these image files.
What are your thoughts on the best way to store these images in my project based on your experience?
I'm using Nginx, DigitalOcean VPS.
Thank you!
Static files are part of your theme / skin ("packaging"). These files should generally not change and should be served based upon when the browser last retrieved them and set far ahead in the future:
location /static {
expires max;
}
Media files are part of the content, not the packaging of the site. It's much more likely that an image changes: the first one uploaded doesn't look that good, better manufacturer supplied media, etc. So it makes more sense to base it's expiration upon the modification date:
location /media {
# Optional: if rollbacks are frequent
# if_modified_since before;
expires modified+1w;
}
Regarding the rollbacks: if it's likely that older versions of an image is restored from backup or versioning systems, you'll want to set this. Note that Django will not set the modification time of an uploaded image to the modification time on the uploader's computer, so a new upload will be a new image, even if it's a previous version.
Now here comes the tricky part: page optimization tools will tell warn you about the expiration time of images not being far in the future and thus will complain about the media files. This is because they can't make a distinction between packaging and content when it comes to images.
Another thing to note is that after that week, the browser will request the images each time and nginx will serve a 304 response when it hasn't changed. Thus your page will generate many more requests. This and the page optimization tools are the two reasons why modified isn't used much for images in the wild and instead only the first policy is used. To deal with modified images, you will then associate a differently named image with the same resource (product) and thus is the responsibility of the content managers or a middleware layer that renames uploaded images to a unique name. One simple trick is to name media images as their md5 or sha1 hash.
Even if you apply a single expiration policy for serving media and static, I would still serve them from different directories. The reason is that static files do not have to be writable by the user running Django (only for the user running the collectstatic management command). This prevents configuration errors or a compromized Django user from messing with the static directory.
I installed Django and enabled the admin site. When I go to the admin page, I get the following
The image does not look the official Django tutorial. In settings.py I updated TEMPLATE_DIRS with the correct path.
TEMPLATE_DIRS = (
"/var/www/mysite/templates/admin"
)
I also tried restarting Apache many times. Any suggestions on what I might be doing wrong? Thank you.
It's an issued related to static files rather than templates:
https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production
Use the developer tools on your browser to looks at what requests are being made. Most likely some static files like CSS, etc. are not being served up.
I've recently have taken a new server under my wing that has an interesting installation of django on it. The previous developer mixed in the media uploads with the static content and in other modules created it's own directory on the root file level of the project. My first reaction to this was general annoyance. ( I'm a huge fan of modular development. ) However after working to 'correct,' it's raised a question.
Even though this question is tagged with django, feel free to post response according to java and asp.net.
How do you set up your static files? Do you stack everything inside a static directory or do you take the time link each modular independently?
One of my tricks for every django app I start is, in the init.py of said app I put the following.
import os
from django.conf import settings as djsettings
TEMPLATES_DIR = (os.path.join(os.path.dirname(__file__),'./templates'),)
djsettings.TEMPLATES_DIR += TEMPLATES_DIR
I don't think your trick is really needed (anymore).
If you use django.template.loaders.app_directories.Loader (docs)
you can put a template dir in your app dir and Django will check it for your app-specific templates
And starting with Django 1.3 you can use the staticfiles app to do something similar with all your static media files. Check the docs for the staticfiles-finders
So finally, thanks to the collectstatic management command, you're now able to keep your static media files modularized (for easier development and distribution), yet you still can bundle them at a centralized place once it is time to deploy and serve your project.
Right now I'm in the habit of putting a static folder in each app directory containing its static files. I keep templates in each app directory under templates. I alias the static path when putting the app behind nginx or apache.
One thing that I'm starting to do more of is putting static files such as javascript, css, or images behind a CDN like S3.
Background:
I'm starting to use Django for the first time, which is also my first foray into web development. I just got stuck on the whole "serving static media" problem. After spending a while looking at all the documentation and StackOverflow questions, I think I understand how it's supposed to work (i.e. MEDIA_ROOT, MEDIA_URL, updating the urls file, etc).
My Question:
Ok, so here's the part I'm not sure about. Django applications are supposed to be "pluggable", i.e. I can move an application from one project to another. So, how should these applications bundle static media?
For example, let's say I have a "foo" application, which has templates that load some css/image files. Where am I supposed to put these files, so that they'll automatically get served once I include the application?
The only solution I see, is that installing an application has to include the extra step of copying its static media to some place on your own server that serves that media.
Is this the accepted way to do it? It includes an extra step, but maybe that's standard when dealing with web-dev (I'm new so I don't really know).
Also, if this is the way, is there a standard way to collect all my static media to make it easy to know what I need to serve? (I.e., is it standard to have a folder named "media" or something inside the app?).
Thanks,
Convention is to put static media in either media/appname/ or static/appname/ within the app (similar to templates).
For using apps in your project that come with media, I strongly recommend using django-staticfiles. It will automatically serve media (including media within apps) in development through a view that replaces django.views.static.serve, and it comes with a build_static management command that will copy media from all apps into a single directory for serving in production.
Update: django-staticfiles has become part of Django 1.3. It now expects app media to live in a "static/" subdirectory of the app, not "media/". And the management command is now "collectstatic."
The only app I know of that deals with this without any intervention is the rather wonderful django-debug-toolbar, though it's arguable that this isn't a great example, since it's an app specifically designed for debug mode only.
The way it deals with it is that it serves its media through Django itself - see the source for urls.py:
url(r'^%s/m/(.*)$' % _PREFIX, 'debug_toolbar.views.debug_media'),
In general, this is a bad idea (you don't want to serve static files through Django), per this comment from the documentation:
[Serving static files through Django] is inefficient and
insecure. Do not use this in a
production setting. Use this only for
development.
Obviously, the django-debug-toolbar is only used for development, so I think its method of deployment makes sense, but this is very much an exception.
In general, the best way I know to do it is to create symbolic links wherever your media is stored to the media inside your app code. For example, create a folder called media within your app, and then require users installing your app to either add a symbolic link from their media directory, or copy the whole thing.
i usually put apps media in ./apps/appname/static (my apps resides in an apps subfolder)
then i have something similar in the vhost in apache :
AliasMatch ^/apps/([^/]+)/static/(.*) /home/django/projectname/apps/$1/static/$2
<DirectoryMatch "^/home/django/projectname/apps/([^/]+)/static/*">
Order deny,allow
Options -Indexes
deny from all
Options +FollowSymLinks
<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf|txt|htm|html|json)$">
allow from all
</FilesMatch>
</DirectoryMatch>
i also have this in my urls.py for dev server (use only for debug) :
def statics_wrapper(request, **dict):
from django.views import static
return static.serve(request, dict['path'], document_root = os.path.join(settings.BASE_DIR, 'apps', dict['app'], 'static'), show_indexes=True)
urlpatterns += patterns('', (r'^apps/(?P<app>[^/]+)/static/(?P<path>.+)$', statics_wrapper))
this is very handy because statics url are simply mapped to filesystem, eg :
http://wwww.ecample.com/apps/calendar/static/js/calendar.js resides in [BASE_DIR]/apps/calendar/static/js/calendar.js
hope this helps