I had a small problem where the datetime.datetime.now() call was giving me the wrong time - in my case, the time was always 1 hour behind. It turned out that the problem had to do with my Django timezone settings.
This fix is easy, but took my a little Googling to figure out so I thought I'd document it.
To start, step into python and check if your time is sync'ed up to GMT.
$ python
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2012, 6, 25, 15, 1, 10, 95084)
If everything is ok, then head into you myproject/settings.py file and look out for the line with the parameter ...
TIME_ZONE = [your time zone]
In my case, that line looks like.
TIME_ZONE = 'America/Toronto'
And that's it. datetime.datetime.now() now returns the right time in my Django code.
How-To's and other posts
Monday 25 June 2012
Friday 8 June 2012
Django and JSON - Easily Create a JSON Response
A simple guide on how to create a JSON response from Django.
Special thanks to these guys - http://stackoverflow.com/questions/2428092/django-python-creating-a-json-response.
---
some_key = '0'
response_data = dict()
nested_response_data = dict()
nested_response_data['result'] = 'failed'
nested_response_data['message'] = 'You messed up'
response_data[some_key] = nested_response_data
return HttpResponse(json.dumps(response_data), mimetype="application/json")
Special thanks to these guys - http://stackoverflow.com/questions/2428092/django-python-creating-a-json-response.
---
some_key = '0'
response_data = dict()
nested_response_data = dict()
nested_response_data['result'] = 'failed'
nested_response_data['message'] = 'You messed up'
response_data[some_key] = nested_response_data
return HttpResponse(json.dumps(response_data), mimetype="application/json")
Tuesday 15 May 2012
How-To: Setting up Ec2 and Django
This took me a weekend to figure out so I thought I'd share.
This post will be a step by step guide on installing Django 2.7 on Ec2 (Ubuntu 12.04 LTS, Apache 2.7 with mod_wsgi).
Firstly, some links that were really handy for me.
- http://simonsstuffandthings.blogspot.ca/2011/02/how-to-install-django-on-ubuntu-using.html
- http://ericwconner.com/?p=347
- http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
- https://docs.djangoproject.com/en/1.4/intro/
And now, on to the tutorial. Hope someone find it useful!
---
1. Launch instance on Ec2 - Ubuntu Server 12.04 LTS (ami-a29943cb) - and do the usual security group (ports 22 and 80) and elastic IP settings. Now SSH into your machine.
2. Update apt-get
$ sudo apt-get update
3. Install Apache
$ sudo apt-get install apache2
4. Install mod_wsgi for apache2.
$ sudo apt-get install libapache2-mod-wsgi
5. Download and install setuptools for Python
$ wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
$ tar xzf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11
$ sudo python setup.py install
6. Download and install pip
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo python get-pip.py
7. Install Django
$ sudo pip install django
8. Time to set up a test project, a test site, and the admin site.
9. Create your Django project. In this example, it will be called "myproject", and located in "/usr/local/src/".
$ sudo django-admin.py startproject myproject
10. Make sure httpd.conf looks like below.
$ sudo vi /etc/apache2/httpd.conf
--- /etc/apache2/httpd.conf ---
Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/
WSGIScriptAlias / /var/www/apache/django.wsgi
<Directory /var/www/apache>
Order deny,allow
Allow from all
</Directory>
--- end ---
Note, "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/" might be different depending on your installation of Python and Django.
11. Create the django.wsgi referenced in httpd.conf
$ sudo mkdir /var/www/apache
$ sudo vi /var/www/apache/django.wsgi
--- /var/www/apache/django.wsgi ---
import os, sys
root = os.path.join(os.path.dirname(__file__), '/usr/local/src')
sys.path.insert(0, root)
sys.path.append('/var/www')
sys.path.append('/usr/local/src/myproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
--- end ---
12. Change permissions for necessary directories
$ sudo chmod o+r /var/www/apache/django.wsgi
$ sudo chmod o+rx /var/www/apache
$ sudo chmod o+rx /usr/local/src
Note, "/usr/local/src" is the directory that hosts all your Django projects and apps
13. Put this line at the end of "/etc/profile" and "/etc/bash.bashrc"
export PYTHONPATH=/usr/local/src/myproject:$PYTHONPATH
export PYTHONPATH=/usr/local/src:$PYTHONPATH
Note, these lines will change depending on which directory you choose to host your Django project and apps.
14. Restart Apache (you need to do this every time you make Django changes too)
$ sudo /etc/init.d/apache2 restart
15. Go to your website, and you should see the Django "It worked!" page.
16. Now let's set up our mySQL database and its Python adapter.
$ sudo apt-get install mysql-server
$ sudo apt-get install python-mysqldb
17. Log in to mysql and create a database for "myproject"
$ mysql -u root -p
CREATE DATABASE myproject;
GRANT ALL ON myproject.* TO root@localhost;
18. Connect your Django project to your database by editing myproject/settings.py and its database settings
19. Sync your project with your database. Now the database is hooked up.
$ sudo python manage.py syncdb
20. Enable the Django admin site.
21. Go to "myproject/myproject/settings.py" and add "django.contrib.admin" to the INSTALLED_APPS settings. Also add "django.contrib.auth" and "django.contrib.contenttypes"
22. Go to "myproject/myproject.urls.py" and uncomment the following 3 lines to enable admin
(1) from django.contrib import admin
(2) admin.autodiscover()
(3) url(r'^admin/', include(admin.site.urls)),
23. Set up permissions
$ sudo chmod o+r /var/www/apache/django.wsgi
$ sudo chmod o+rx /var/www/apache
24. Now go to "http://<YOUR_IP>/admin" to see your admin site. And that's it!
Thanks for reading!
This post will be a step by step guide on installing Django 2.7 on Ec2 (Ubuntu 12.04 LTS, Apache 2.7 with mod_wsgi).
Firstly, some links that were really handy for me.
- http://simonsstuffandthings.blogspot.ca/2011/02/how-to-install-django-on-ubuntu-using.html
- http://ericwconner.com/?p=347
- http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
- https://docs.djangoproject.com/en/1.4/intro/
And now, on to the tutorial. Hope someone find it useful!
---
1. Launch instance on Ec2 - Ubuntu Server 12.04 LTS (ami-a29943cb) - and do the usual security group (ports 22 and 80) and elastic IP settings. Now SSH into your machine.
2. Update apt-get
$ sudo apt-get update
3. Install Apache
$ sudo apt-get install apache2
4. Install mod_wsgi for apache2.
$ sudo apt-get install libapache2-mod-wsgi
5. Download and install setuptools for Python
$ wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
$ tar xzf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11
$ sudo python setup.py install
6. Download and install pip
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo python get-pip.py
7. Install Django
$ sudo pip install django
8. Time to set up a test project, a test site, and the admin site.
9. Create your Django project. In this example, it will be called "myproject", and located in "/usr/local/src/".
$ sudo django-admin.py startproject myproject
10. Make sure httpd.conf looks like below.
$ sudo vi /etc/apache2/httpd.conf
--- /etc/apache2/httpd.conf ---
Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/
WSGIScriptAlias / /var/www/apache/django.wsgi
<Directory /var/www/apache>
Order deny,allow
Allow from all
</Directory>
--- end ---
Note, "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/" might be different depending on your installation of Python and Django.
11. Create the django.wsgi referenced in httpd.conf
$ sudo mkdir /var/www/apache
$ sudo vi /var/www/apache/django.wsgi
--- /var/www/apache/django.wsgi ---
import os, sys
root = os.path.join(os.path.dirname(__file__), '/usr/local/src')
sys.path.insert(0, root)
sys.path.append('/var/www')
sys.path.append('/usr/local/src/myproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
--- end ---
12. Change permissions for necessary directories
$ sudo chmod o+r /var/www/apache/django.wsgi
$ sudo chmod o+rx /var/www/apache
$ sudo chmod o+rx /usr/local/src
Note, "/usr/local/src" is the directory that hosts all your Django projects and apps
13. Put this line at the end of "/etc/profile" and "/etc/bash.bashrc"
export PYTHONPATH=/usr/local/src/myproject:$PYTHONPATH
export PYTHONPATH=/usr/local/src:$PYTHONPATH
Note, these lines will change depending on which directory you choose to host your Django project and apps.
14. Restart Apache (you need to do this every time you make Django changes too)
$ sudo /etc/init.d/apache2 restart
15. Go to your website, and you should see the Django "It worked!" page.
16. Now let's set up our mySQL database and its Python adapter.
$ sudo apt-get install mysql-server
$ sudo apt-get install python-mysqldb
17. Log in to mysql and create a database for "myproject"
$ mysql -u root -p
CREATE DATABASE myproject;
GRANT ALL ON myproject.* TO root@localhost;
18. Connect your Django project to your database by editing myproject/settings.py and its database settings
19. Sync your project with your database. Now the database is hooked up.
$ sudo python manage.py syncdb
20. Enable the Django admin site.
21. Go to "myproject/myproject/settings.py" and add "django.contrib.admin" to the INSTALLED_APPS settings. Also add "django.contrib.auth" and "django.contrib.contenttypes"
22. Go to "myproject/myproject.urls.py" and uncomment the following 3 lines to enable admin
(1) from django.contrib import admin
(2) admin.autodiscover()
(3) url(r'^admin/', include(admin.site.urls)),
23. Set up permissions
$ sudo chmod o+r /var/www/apache/django.wsgi
$ sudo chmod o+rx /var/www/apache
24. Now go to "http://<YOUR_IP>/admin" to see your admin site. And that's it!
Thanks for reading!
Subscribe to:
Posts (Atom)