so I'm building a basic site to learn django, and i am currently setting up the admin site. when launch the site through
python manage.py runserver
i can go to the website and it looks pretty just like this. http://cl.ly/2Z3A2n0I1E140X3k180h
but when i load the site through my apache web server it looks like this. http://cl.ly/3H3r1G2D193p462t3641
can anyone help me?
it looks to me like the admin site is not able to load the proper template on the live site. but i did not setup any special template. and when i look at the permissions of the template folder it seems to be fine... maybe not. any help?
Ok, sorry for the long story:
First, find out where your django installation rests:
in the CLI:
>>> import django
>>> django
<module 'django' from '/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'>
The this is the path (except for the __init__.pyc, of course) to your Django installation.
Now, in your media directory, you could create a symbolic link to the directory that the admin media is located in (this way you will not have to copy the files to your media directory).
Assuming that you are in your media directory use this command to create dynamic link to the admin media directory (the first argument to ln -s should of course be your django path that we got retrieved earlier):
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/ admin
Now in your settings.py you can use something like this:
MEDIA_URL = 'media'
ADMIN_MEDIA_PREFIX = '/media/admin/
You will also have to add this directive to your Apache config:
Alias /media/ /full/path/to/your/django/site
Related
My django admin page didn't display properly. Like a pure html display. Can someone help here?
I don't have the permission to upload screenshot. But you can imagine, it doesn't look the same as tutorial shows.
thanks
You probably don't have static files serving set up. If you have DEBUG turned on, you'll get this automatically with Django's development server (./manage.py runserver). If running in production, you need to set this up manually. See the docs for more info.
Basically your static files are not set up properly, some of the reasons can be :-
If you running on a server like apache, you need to have static url in settings.py and same alias in httpd.conf file of apache.
If you are running on django in built server, please try to set Debug=True, which will force django to serve those static files.
If you have debug=False in settings.py file, https://docs.djangoproject.com/en/dev/howto/static-files/
Because u really need a static files from django, sometimes you have to set the alias of django admin static files separately, which can be set in urls.py that is pointing to django package
found the issue.
I forgot to turn on
'django.contrib.staticfiles'
in setting.py file
How do I resolve? I don't want to push a copy of Django Admin's CSS to my static files unless I have to. Is that my only option or ??
Once you move your application to production, in addition to setting DEBUG = False in your settings.py, you also have to run collectstatic and then upload these files to your webserver.
collectstatic will put all the static files that are required for all installed applications, including the django.contrib apps (like the django admin), into the folder you specified as STATIC_ROOT. You should then copy the entire contents of this folder to wherever your STATIC_URL is pointing to.
If you don't do the above, your stylesheets and other assets will not appear correctly.
As you are using S3, the excellent django-extensions package provides a sync_s3 command that will handle synchronizing your bucket for you.
Starting from django 1.6, there is an official list of things you should do before you are ready to deploy; so if you are on the current version of django make sure you visit that page. It is also pointed to in the comments in settings.py.
in Django 1.3 you could set STATIC_URL to the S3 URL and ADMIN_PREFIX to your local server, creating a webserver alias that serves the files
(in Apache for example: Alias /static/admin/ /absolute/path/to/static/admin/)
but AFAIK this is deprecated in Django 1.4 and not possible with Django >= 1.5, since they always will point to {{STATIC_URL}}admin/.
but i don't understand, why wouldn't you upload your admin static!? i strongly suggest to colletstatic all your media to S3 :)
(i don't know if i understood you question correctly, i'll update the answer if not)
I'm trying to deploy a small django app on heliohost following the instructions in this post How to deploy a Django site using a different version of Django on Heliohost and also the official ones. But when I try to browse the site I only get the index of my public_html folder. Does anyone have any idea what I am doing wrong?
I know this is old post but if you still trying django on heliohost, try cookiecutter template for heliohost.
$ pip install cookiecutter
$ cookiecutter https://github.com/rahul-gj/cookiecutter-helio.git
project_name [mysite]: ---> Type your project name here.
helio_user [user_name]: ---> your heliohost username.
The folder with project name will be created in your working directory.
Paste the content of the folder (folder + manage.py file) on heliohost public_html directory.
go to yourusername.heliohost.org/yourproject
You should see something like this:
https://krydos.heliohost.org/djangotest/
Explore the django on heliohost.
I have the following model field in a django project:
headshot = models.ImageField(upload_to='/tmp', blank=True, null=True)
settings.py
# format <project>/<app>
MEDIA_ROOT = '/home/username/webapps/django/myproject/books/media/'
I have created a directory: /home/username/webapps/django/myproject/books/media/tmp and gave it chmod 777 permissions.
I get the following error when I try to add a new image:
SuspiciousOperation at /admin/books/author/add/
Attempted access to '/tmp/Comment.png' denied.
Request Method: POST
Request URL: http://username.webfactional.com/app/admin/books/author/add/
Django Version: 1.3.1
Exception Type: SuspiciousOperation
Exception Value:
Attempted access to '/tmp/Comment.png' denied.
Exception Location: /home/username/webapps/django/lib/python2.7/django/core/files/storage.py in path, line 234
Python Executable: /usr/local/bin/python
Python Version: 2.7.1
Does anyone know what I'm doing wrong? Do I need .htaccess or some other settings to be able to write to this directory or is there something wrong with my Django config?
I would still like to know what chmod values to use and how to set MEDIA_URL. I'm thinking I might need to do something in urls.py for MEDIA_URL.
My project is set up on the webhost Webfaction if that makes a difference.
Fixed it. Seems like the answer was to use upload_to='tmp' instead of upload_to='/tmp'.
I see you've marked this as answered, but still have some questions in your answer. Here is how I have this set up, in case you are still working on it.
Set up a new app for media (via the webfaction control panel). I used the "static_only" application because it should prevent users from uploading scripts and running them. You could also use one of the "static/cgi/php". Creating a new app for this (instead of using Django and making a new view) is good practice because it will have a lower resource overheard. Django is overkill for serving static files (images, css files, javascript files, etc.).
Bind your new media (static_only, or whatever you used) app to some URL, again using the webfaction control panel. I bound mine to media..com. MEDIA_URL should be set to the URL you can access your media through, and MEDIA_ROOT should be the root directory for the media app. Thus, in my settings.py file I have:
MEDIA_ROOT = '/home/<username>/webapps/media/'
MEDIA_URL = 'http://media.<mydomain>.com/'
It's best to do the same thing for STATIC_ROOT and STATIC_URL.
As far as permissions go, the defaults should be OK - if you have troubles with that just ask.
I would like to make CSS-only adjustments to the admin interface (on an RHEL box I don't have sysadmin privileges to). To that end, I would like a local version of /media/ to tweak.
http://docs.djangoproject.com/en/dev/howto/static-files/ (but not http://docs.djangoproject.com/en/1.2/howto/static-files/) suggests running a manage.py collectstatic or manage.py findstatic, and my Django 1.2.3 manage.py does not recognize those commands. Adding 'django.contrib.staticfiles' to my INSTALLED_APPS also broke things (not found).
I would like to customize the CSS, and the way I envision doing that is by getting a private copy of the media for Django's admin and changing from there. What are my best options for a Django 1.2.3 installation?
Just go to your django installation directory, browser to django->contrib-> admin-> directory and copy paste the media folder to your django projects media directory.
Let us say you have your media in project/media which is statically rendered to web as /media/. If you copy your admin media in project/media your admin media web url would be /media/admin/ hence in your settings.py file: ADMIN_MEDIA_PREFIX = '/media/admin/'
You may then modify your css in media directory as you wish to change the look of admin site.
I found a way to do it:
1: Download a fresh copy of Django and unpack it. Under the Django-x.x.x directory was a media directory.
2: Tar/transfer/untar the media directory on my target machine.
3: Change the CSS as needed.
4: When calling python manage.py runserver, add a flag of --adminmedia=./media .