No Style/css On Django Admin - django

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.

Related

Setting relative links

I just bought from a marketplace a template for my site. The thing is that this template has relative links to its resources (styles, images, scripts, etc). Of course, all the links are currently broken. My question is: what could be the best approach to fix these links?.
I don't thing that setting all the links to {{ STATIC_URL }} be a good idea since this template has a lot of files, resources and links.
Note: When I say "template" I'm meaning a folder with a lot of HTML, CSS and images files.
Thanks in advance!
Update:
This is how my problem looks: https://www.dropbox.com/s/gbtfjaaaqxw7coo/error.png
Solved, I just added to my urls.py this:
urlpatterns += patterns(
'django.contrib.staticfiles.views',
url(r'assets/(?P<path>.*)$', 'serve'),
)
And set STATIC_URL = 'assets/'

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.

Django trouble serving media files

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/

How can I access the WSGIScriptAlias from my django settings file?

I have this line in my wsgi.conf file:
WSGIScriptAlias /trunk "c:/app/trunk/app.wsgi"
Inside of my django settings file, I need to know the alias "/trunk" to get LOGIN_URL to work properly. How can I retrieve this value from my apache settings?
Thanks!
Pete
Access the original WSGI environ dictionary for a specific request and lookup the 'SCRIPT_NAME' variable. The value of this is the notional mount point for the WSGI application as specified when using WSGIScriptAlias. Getting it through the per request environment is the only real way of doing it automatically. You cannot get to it from outside of a request and there should be no real need for you to do that.
By rights, Django should provide a way of automatically having the mount point of the application inserted into configured URLs such as that. You should perhaps bring up the issue on the official Django users list instead if you cannot find the way of doing it as perhaps a change in Django is needed.
Since you want to obtain a value from the Apache configuration, I guess the only thing you can do is read the file and process it.
Something like (assuming your settings.py lives in the same directory as wsgi.conf):
try:
f = open('wsgi.conf', 'r')
LOGIN_URL=[line for line in f.readlines() if 'WSGIScriptAlias' in line][0].split()[1]
finally:
f.close()
Catching an exception if the file is not there might be a good idea too.
Edit after your comment: Ah, I see what you are trying to do. This thread may be helpful in understanding why using os.environ won't work. The alternative they present won't help you though:
In a nutshell, the apache SetEnv isn't setting values in the process
environment that os.environ represents. Instead SetEnv is setting
values in the context of the WSGI request.
Within your code you can reference that context at request.environ:
def myView(request):
tier = request.environ['TIER']
It's me again. Because of what Apache's SetEnv is doing, I don't think you will have access to the variable in settings.py. It seems parsing the file is still the only option.
Further Edit: Another alternative - can you base your decision on the host name?
#settings.py
import socket
production_servers = { 'server1.com': '/trunk...',
'server2.com': '/trunk_2...' }
LOGIN_URL=production_servers[ socket.gethostname() ]
This completely sidesteps the information contained in apache configuration.