Hosting static website with Django - python

I have Django app running live on AWS at, www.domain.com/admin. It doesn't posses any html pages, we only make use of Django-Admin.
Now I have to host a website at www.domain.com.
I have my website package in this form,
site
|-sass
|-js
|-img
|-fonts
|-css
|-index.html
I copy-pasted my site folder inside my Django app at my_django_app/templates/
Also, added this :
url(r'^$', TemplateView.as_view(template_name='site/index.html')),
inside my_django_app/my_django_app/urls.py.
And, updated my settings.py with,
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'templates/site'),
]
Now when I runserver and go to www.domain.com, it loads my html file without CSS, JS and Images.
Please suggest me what I am doing wrong.
I am a beginner to python and also never hosted a website ever before.

This answer is a reply to this comment, not an answer to the original question.
In your Apache config, change: WSGIScriptAlias / to WSGIScriptAlias /admin.
Add this before the </VirtualHost> closing tag:
Alias / /path/to/static/site/
<Directory /path/to/static/site/>
Options Indexes FollowSymLinks
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
I haven't tested it, but I believe this should work.

Related

Django can upload files to /media/ but gives 404 in production

I just looked at others threads such as Can upload Django media, can't retrieve but I did not find the answer to my question. I also followed https://docs.djangoproject.com/fr/1.9/howto/deployment/wsgi/modwsgi/ without any success...
I just passed my website from development to production and since then, every uploaded files is providing a 404 mistakes when I try to access it from webpages. The strange thing is that they are all in the /media/ folders on the servers, therefore I am able to upload them without trouble.
What is wrong in my configuration ?
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Media Files (Uploaded pictures, etc)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'upload')
urls.py
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
apache2/sites-enabled/*.conf
Alias /media/ /var/www/SIS/media/
<Directory /var/www/SIS/media/>
Require all granted
</Directory>
It only affect the /media/ folders, all static files are served well.
Many thanks !

Django deployment, all links are prepended with index.wsgi

I want to deploy my django app in a Ubuntu server with Apache and mod_wsgi. I am following this tutorial
http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
in my site file I have
<VirtualHost *:80>
ServerAdmin webmaster#mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
WSGIScriptAlias / var/www/index.wsgi
Alias /static/ /var/www/static/
<Location "/static/">
Options -Indexes
</Location >
</VirtualHost >
Don't mind the generic mydomain.com entries. The site is for progress display only so I can discuss it with my friend.
in /var/www/index.wsgi I have this
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/voger/venvs/ssite/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/home/voger/projects/ssite')
sys.path.append('/home/voger/projects/ssite/ssite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ssite.settings'
# Activate your virtual env
activate_env=os.path.expanduser("/home/voger/venvs/ssite/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
in my settings.py I have two lines for redirecting after login and logout. These are not prepended with index.wsgi
LOGIN_REDIRECT_URL = '/login_redirect'
ACCOUNT_LOGOUT_REDIRECT_URL = "/logout_redirect"
When these urls are requested it returns 404
Reverse for 'login_redirect' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
In my html I have 4 links
<a id="logout" style="display: inline-block;" href="/index.wsgi/accounts/logout/">Logout</a>
<a id="profile" style="display: inline-block;" href="/accounts/profile">My Account</a>
<a id="signup" style="display: none;" href="/index.wsgi/accounts/signup/">Sign Up</a>
<a id="login" style="display: none;" href="/index.wsgi/accounts/login/">Sign In</a>
and as you can see 3 entries that are inserted with {% url %} are prepended automatically with /index.wsgi while the only one who is hardcoded it is not prepended. But still they work as expected.
Why do I have this /index.wsgi prepended and how can I remove it? In my development machine using django's built in server everything works fine.
I don't know if it is related but the login, logout and signup urls are provided by django-allauth.
Thank you for any answer or comment
For starters:
WSGIScriptAlias / var/www/index.wsgi
is wrong. You are missing leading slash on the path for the WSGI script.
You also should not really go sticking index.wsgi in DocumentRoot directory as you possibly are. If for some reason your VirualHost with WSGIScriptAlias isn't being used, and a .wsgi handler of wsgi-script has been setup elsewhere in the Apache configuration, it means that a URL of /index.wsgi works to access your Django with all your problems then starting from there. This is because Django will believe it is meant to prefix URLs with /index.wsgi as that ends up being the mount point of the application.
So, move your WSGI script file out of DocumentRoot directory for your server. Remove any AddHandler directive for .wsgi extension. Ensure you VirtualHost is actually being used and specifically that WSGIScriptAlias directive usage is correct.
BTW, why are you using some arbitrary persons blog post rather than the official mod_wsgi setup notes on the Django docs site.
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/

Pop-ups in django-tinymce editor showing 404 error

What is wrong:
I have installed django-tinymce to work with text fields in the admin section of my site. The editor works good, though not as expected, because whenever I try to insert an image, link, symbol etc. - that is, whenever a pop-up is ensued - editor opens a 404 Not found window instead of actual widget form.
What I have tried:
I 've tried adding
document.domain = 'my_site.com';
to tiny_mce_popup.js. Nothing changed.
Interestingly enough, the *.htm files, which are supposed to open in pop-up windows, are stored in the directory {{ MEDIA_URL }}js/tiny_mce/themes/advanced/ (as I've mentioned, server doesn't open them). But if I try and open in browser other files from that very same directory (e.g. {{ MEDIA_URL }}js/tiny_mce/themes/advanced/editor_template.js or {{ MEDIA_URL }}js/tiny_mce/themes/advanced/img/flash.gif), everything works - the files are displayed without any error.
In addition, on local server everything works just fine - the problem is encountered only after deployment.
Code that might help identifying the problem:
Project's urls.py:
urlpatterns = patterns('',
#...
url(r'^tinymce/', include('tinymce.urls')),
#...
)
TinyMCE's urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('tinymce.views',
url(r'^js/textareas/(?P<name>.+)/$', 'textareas_js', name='tinymce-js'),
url(r'^js/textareas/(?P<name>.+)/(?P<lang>.*)$', 'textareas_js', name='tinymce-js-lang'),
url(r'^spellchecker/$', 'spell_check'),
url(r'^flatpages_link_list/$', 'flatpages_link_list'),
url(r'^compressor/$', 'compressor', name='tinymce-compressor'),
url(r'^filebrowser/$', 'filebrowser', name='tinymce-filebrowser'),
url(r'^preview/(?P<name>.+)/$', 'preview', name='tinymce-preview'),
)
TinyMCE's settings.py:
import os
from django.conf import settings
DEFAULT_CONFIG = getattr(settings, 'TINYMCE_DEFAULT_CONFIG',
{'theme': "advanced", 'relative_urls': False})
USE_SPELLCHECKER = getattr(settings, 'TINYMCE_SPELLCHECKER', False)
USE_COMPRESSOR = getattr(settings, 'TINYMCE_COMPRESSOR', False)
USE_FILEBROWSER = getattr(settings, 'TINYMCE_FILEBROWSER',
'filebrowser' in settings.INSTALLED_APPS)
JS_URL = getattr(settings, 'TINYMCE_JS_URL',
'%sjs/tiny_mce/tiny_mce.js' % settings.MEDIA_URL)
JS_ROOT = getattr(settings, 'TINYMCE_JS_ROOT',
os.path.join(settings.MEDIA_ROOT, 'js/tiny_mce'))
JS_BASE_URL = JS_URL[:JS_URL.rfind('/')]
So how do I make django-tinymce's pop-ups work? Thanks in advance for your help!
EDIT: Solution found. Turns out my hosting provider didn't include htm(l) in allowed static files extensions list. Now everything's working.
The fact that it is working under the development server, but not live, leads me to think that you haven't set up the media root properly in your apache conf. The development server automatically serves your media and static files but for your live deployment you need to add the media and static aliases with the relevant path.
vhost.conf:
<VirtualHost *:80>
# Your setup for your main site url, then add the below to allow access
# to the media and static roots
Alias /media/ "/path/to/myproject/media/"
<Directory "/path/to/myproject/media/">
Order deny,allow
Allow from all
</Directory>
Alias /static/ "/path/to/myproject/static/"
<Directory "/path/to/myproject/static/">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

Django admin page is missing images/css/js

I have found a few questhons re my problem, you the solutions didn't help me so I started a new question. Basically that's how I see the admin page
Apache's config:
<VirtualHost *.*.*.*:80>
ServerName ********.org
ServerAdmin ****#******.org
WSGIScriptAlias / /var/www/webproxy/webproxy/wsgi.py
DocumentRoot /var/www/cgi-bin/
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
ErrorLog /var/www/webproxy/apache/error.log
LogLevel warn
CustomLog /var/www/webproxy/apache/access.log combined
Alias /media/ "/var/www/webproxy/media/"
<Directory "/var/www/webproxy/media/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory "/var/www/webproxy/static/">
Order deny,allow
Allow from all
</Directory>
This is what I have in urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from core import urls as core_urls
from settings import MEDIA_ROOT, WEBPROXY_MEDIA_ROOT
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}),
(r'^ui/(?P<path>.*)$', 'django.views.static.serve', {'document_root': WEBPROXY_MEDIA_ROOT}),
)
settings.py:
MEDIA_ROOT = '/var/www/webproxy/media/'
STATIC_ROOT = '/var/www/webproxy/static/'
WEBPROXY_MEDIA_ROOT = '/var/www/webproxy/static/media/'
I'm not sure what's wrong here. Any help would be really appreciated.
First, you shouldn't be serving the static files via django.views.static.serve in production as that is meant to be used only for local development (read docs here):
There may be files other than your project's static assets that, for
convenience, you'd like to have Django serve for you in local
development.
Second, your Django Admin static files reside under the location Django was installed at. I find using directly these files a better solution than copying (or symlink'ing) the Django admin static files under my own static files directories.
Example Django static files location on my server:
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
Therefore in your web server config you have to point the url /media/admin to
[my_django_location]/contrib/admin/static/admin/
If you're doing things locally, then you might need another entry in your urls.py:
(r'^admin/static/(?P<path>.*)$', 'django.views.static.serve', '/path/to/your/admin/files/'),

Redirecting Django root to admin panel

I'm trying to redirect the Django site root to the Django admin panel, and I've mostly got it up and running. However, I've noticed that unless there's a trailing slash in the project URL, the links on the page don't include the project folder, and instead try to find the apps in the site root.
So, say I've got a project foo, and an app bar. If you visit http://server/foo/ (with a trailing slash), everything works fine, and the links on the page go to http://server/foo/bar. However, if one visits http://server/foo, the generated links go to http://server/bar instead, which generates a 404 error.
If I set the WSGIScriptAlias to point to /foo/ instead of /foo, it would give a 404 error if I navigated to /foo. I tried to force a trailing slash in the Apache conf with Redirect, but I end up generating a recursive redirect (http://server/foo//////...). I haven't yet tried using a .htaccess file, but I suspect the same thing might happen.
I tried the same thing in urls.py, however:
url(r'^$', redirect_to, {'url': '/'}), # goes to server root http://server/
url(r'^$', redirect_to, {'url': 'foo/'}), # goes to http://server/foo/foo
url(r'^$', redirect_to, {'url': '/foo/'}), # infinite redirect
I also tried simply appending a slash to all the Django urls like so:
url(r'^(.*)/', include(admin.site.urls))
But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).
I'm using Apache 2.2 with mod_wsgi, here is the configuration:
Alias /static "C:/DJANGO~1/apps/django/django/contrib"
<Directory 'C:/DJANGO~1/apps/django/django/contrib'>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias /foo "C:/Djangostack/apps/django/scripts/django.wsgi"
<Directory 'C:/Djangostack/apps/django/scripts'>
Order allow,deny
Allow from all
</Directory>
And this is the urls.py that mostly works:
urlpatterns = patterns('',
url(r'^', include(admin.site.urls)),
)
I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.
Try setting APPEND_SLASH to True in your settings.py. I had a similar problem and this fixed it for me. It's supposed to default to True but I had to set it explicitly.

Categories

Resources