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.
Related
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.
I can't get my static files in Django to work with "namespaced" urls - I wonder if I'm missing something here?
my project structure
Basically I have one app called "foo" in my project "static_test". Inside that app I have a "static" folder which contains "test.txt".
I haven't changed anything in settings.py, so it contains all the default settings: staticfiles is included in INSTALLED_APPS, and STATIC_URL is set to '/static/'.
All of this is in full accordance with official Django tutorial: https://docs.djangoproject.com/en/1.9/howto/static-files/
I'm using Django 1.9.2.
When I start the Django development server, this is what I get:
localhost:8000/static/test.txt - 200 OK
localhost:8000/static/foo/test.txt - 404 Page not found
This is despite the official tutorial claiming
In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).
Why is the second url (/static/foo/test.txt) not working for me? And why is the first url working? I do not have a project-wide static folder.
Thanks
from the docs
You should use 'django.contrib.staticfiles.finders.AppDirectoriesFinder' in your STATICIFILES_FINDERS variable in your settings.
First one is searching under static.
Second one under foo under static.
Your link should look like this.
href="{% static "test.txt" %}"
And settings.py like this
STATIC_URL = '/static/'
My initial goal is to use sorl-thumbnail in the most basic way to cache on my filesystem cropped images that are downloaded from external sites. I don't care about performance at the moment and don't want to yet setup extra systems (memcachedb, redis). I am using the development runserver.
On the one hand the docs make it sound like I must use one of these two options. I feel like other places I have read that it can be setup to not require these KV stores. As one evidence for that, I see the setting sorl.thumbnail.kvstores.dbm_kvstore.KVStore in the reference docs (which says A simple Key Value Store has no dependencies outside the standard Python library and uses the DBM modules to store the data.), but I cannot get that to work either (see below).
Using Python 2.7.5, Django 1.7.1, Pillow 2.6.1, and sorl-thumbnail 12.1c.
Added sorl.thumbnail as part of my INSTALLED_APPS.
Added to settings.py:
THUMBNAIL_DEBUG = True
import logging
from sorl.thumbnail.log import ThumbnailLogHandler
handler = ThumbnailLogHandler()
handler.setLevel(logging.DEBUG)
logging.getLogger('sorl.thumbnail').addHandler(handler)
I see no other logging in my web server console despite this.
Attempted to sync my db:
$ ./manage.py migrate thumbnail
Operations to perform:
Apply all migrations: thumbnail
Running migrations:
Applying thumbnail.0001_initial... FAKED
No tables appear to be added to my database.
At this point, I've added to my template the load directive and the following snippet, where item.image_url is a models.URLField which works fine apart from thumbnail.
{% thumbnail item.image_url "235x200" crop="center" as im %}
<img src="{{ im.url }}">
{% empty %}
<p>No image</p>
{% endthumbnail %}
When I try to view the page, I see broken image links:
http://127.0.0.1:8001/<myapp>/cache/cf/43/cf43126f1f961593650b5df4791e329f.jpg 404 (NOT FOUND)
My MEDIA_URL is not set, though I tried playing with that to no avail.
I further tried putting into the settings: THUMBNAIL_KVSTORE = 'sorl.thumbnail.kvstores.dbm_kvstore.KVStore' but this gives the DJANGO error in the browser: Error importing module sorl.thumbnail.kvstores.dbm_kvstore: "No module named dbm_kvstore".
Can I configure it in this way, not requiring memcached, and if so, which of my settings are wrong/missing? If I must use memcached, how many more settings must I configure in addition to its installation? Thanks.
Update
Here are my settings involving static assets.
STATIC_URL = '/static/'
STATIC_ROOT = '/tmp/static/'
STATICFILES_DIRS = (
PROJECT_ROOT.child("static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder', If enabled breaks my LESS CSS.
'static_precompiler.finders.StaticPrecompilerFinder',
)
# STATICFILES_STORAGE not set but should default to 'django.contrib.staticfiles.storage.StaticFilesStorage'
I am already serving some static images from mysite/static/img. And I forgot to mention that I am using the Django Static Precompiler for LESS CSS. My LESS files are at mysite/static/css and are compiled to /tmp/static/COMPILED/.
I see there is a cache dir in my project root mysite, and it does have the file which is trying to be served: cache/6a/a6/6aa6ebf6cef5bf481fd37d4947d25623.jpg.
I've read the documentation on serving static assets but it's unclear to me what settings to change. Seems that I either need to have that jpg produced in a different directory, or add this directory to the list of dirs from which I'm serving static assets. I tried adding this path to STATICFILES_DIRS but that didn't work.
When you go into this directory '/cache/cf/43/', do you actually see the file 'cf43126f1f961593650b5df4791e329f.jpg' in there?
If so, it may be returning a 404 because you are using the Django runserver (not sure if you are or not). If you are, it might be worth taking a look at how to serve media files in development mode, https://docs.djangoproject.com/en/1.7/howto/static-files/.
You don't need to setup the cache backend initialy, you may need to setup serving the static files, please look at the django docs about serving MEDIA and STATIC resources.
But the most important, Django 1.7 Support was introduced in the 12.1c release.
Try first:
pip install sorl-thumbnail==12.1c
It's also helpful that you set the debug thumbnail setting on your settings file:
THUMBNAIL_DEBUG = True
Both other answers correctly suggested problems with serving up media files. Here is the complete list of code changes that were required:
I had glossed over the fact that MEDIA assets are not the same as STATIC assets. This answer is the one that tipped me off to the fact that sorl-thumbnail is relying on MEDIA_URL to form its URL, and accordingly, MEDIA_ROOT. For my development, I set the following:
MEDIA_URL = '/media/'
MEDIA_ROOT = '/tmp/media/'
I used this snippet for URLconf changes for serving up the media files. At this point I put an image in the media directory and made sure my template could correctly reference it.
By this point, I had manually removed the sorl-thumbnail generated thumbnails, but no permutation of settings and activity would regenerate them. I remembered that getting your key value store/database/cached images out of sync requires manual cleanup. The management command ./manage.py thumbnail cleanup did the job, and it began regenerating again.
Also worth noting is that I did not have to set THUMBNAIL_KVSTORE at all, or setup any key value store.
I hope this helps get others get started faster in setting up their dev environment.
I am trying to get PyBBM 0.15.5 working on Django 1.6.3. Seems easy enough, but I run into a silly small problem which I don't know where to look for.
When PyBBM is trying to load static content, it does not use the correct URL. It uses:
/forum/forum/2/topic/add/pybb/emoticons/shok.png
^(fails)
Instead of:
/static/pybb/emoticons/shok.png
^(works)
Clearly something that generates the URL's based on some variable is not set correctly, but I dont know where to look. The PyBBM app? generic settings?
In my settings for my project, I have a static url setup as follows:
STATIC_URL = '/static/'
The actual static content is in this directory:
/usr/local/lib/python2.7/dist-packages/pybb/static/pybb/
Any hints are appreciated and forgive me my ignorance, as I am a novice coder.
Dennis
I was missing the following context processor:
'django.core.context_processors.static',
After adding these, the STATIC_URL in settings.py was picked up correctly.
However I did also run into another issue, where PyBBM tested for a LANGUAGE_CODE in the context, which was not there. I solved this by adding this context processor:
'django.core.context_processors.i18n',
I have a django test site and I can see the admin site, but it has different formatting from the tutorial examples.
This is how my admin site looks:
But on the example it looks like this:
What is missing to show the correct formatting?
Looks like your CSS file isn't being loaded. Make sure your django install is set up correctly to server out static files. The CSS file, by default, is in /static/admin/css.
uncomment 'django.contrib.staticfiles' under INSTALLED_APPS from settings.py.