Tag Archives: django

Quickly Start a Django Project

There are many things I like to do when starting a django project. I started to compile those in a script I’m using. The gist is linked below.

New Django Project.sh

#!/bin/bash
# 
# Author: Abdallah Deeb 
# Requirements: python, pip, virtualenv, virutalenvwrapper, git
#
# Edit the following 2 lines
PROJECTNAME=proj
APPNAME=myapp
if [ -n "$2" ]
then 
  APPNAME="$2"
fi
if [ -n "$1" ]
then 
  PROJECTNAME=$1
fi

source `which virtualenvwrapper.sh` 

echo "Starting a new Django project: $PROJECTNAME"
# Make a virtualenv
mkvirtualenv $PROJECTNAME

# Install latest django and start the project/app
pip install django
django-admin.py startproject $PROJECTNAME
cd $PROJECTNAME
django-admin.py startapp $APPNAME
mv $PROJECTNAME conf

# conf is much nicer than projname
replace $PROJECTNAME conf -- manage.py conf/settings.py conf/wsgi.py conf/urls.py
chmod +x manage.py
workon $PROJECTNAME

# Initialize and use git
git init
git add .
git commit -a -m 'initial commit'

and the raw gist download

Similar Posts:

Tagged , , , ,

Django on Codio

Codio is awesome. Coolio would have been a better name, but I guess that’s taken!

It’s free, but don’t let that fool you. What you get is a beautifully simple yet powerful IDE in your browser. It works great even on my tablet, so I can edit/update/deploy even on the road (or on the couch half asleep, and here’s where git comes in handy!). It supports github out of the box. And has a cool terminal with SSH and everything. Yay!

Now if that was not awesome enough, these guys went out of their way and added an even cooler feature:

Every project gets its own Box: an instantly available server-side development environment with full terminal access.

So you get to do it all without leaving their site. I wanted to try playing with a pet project I’m working on using django. It was pretty easy to get it all setup (especially that the code was on github, but you could as simply upload the code).

Here’s a step by step tutorial to get django up and running on codio.

Open a terminal:
Tools > Terminal

I’m using MySQL, so:

parts install mysql
parts mysql start
mysqladmin -u root password StrongPassword
mysql -u root -pStrongPassword
mysq> create database dbname;
mysql> grant all privileges on dbname.* to 'dbuser'@'localhost' identified by 'dbpasswd';
mysql> flush privileges;
mysql> \q

Also make sure to install python using the ‘parts’ command:

parts install python2

(see the related issue on github.com: https://github.com/codio/boxparts/issues/80)

Create the requirements.txt file if you don’t have that already. Hint: run ‘pip freeze > requirements.txt’ to save those. So in requirements.txt:

Django
MySQL-python
#distribute
#wsgiref

Run the following in the codio.com terminal:
parts install pip
pip install -r requirements.txt

Upload the django project, or import it from GitHub then:
python manage.py syncdb
python manage.py runserver 0.0.0.0:8009

I still need to get it running through apache. I’ll get on that later tonight.

parts install apache2_mod_wsgi # also installs apache2 and apr_util
============ apache2 ============                  
To start the Apache server:
  $ parts start apache2 
To stop the Apache server:
  $ parts stop apache2    
Apache config is located at:
  $ /home/codio/.parts/etc/apache2/httpd.conf                                               
Default document root is located at:  
  $ /home/codio/workspace 
     
============ apache2_mod_wsgi ============                
If Apache2 httpd is already running, you will need to restart it: 
  $ parts restart apache2
Default configuration for wsgi is:  
  WSGIScriptAlias / /home/codio/workspace                                                  
  You can change default in /home/codio/.parts/etc/apache2/config/wsgi.conf file

So I changed the file in /home/codio/.parts/etc/apache2/config/wsgi.conf to:

LoadModule wsgi_module  /home/codio/.parts/packages/apache2_mod_wsgi/3.4/mod_wsgi.so
WSGIScriptAlias / /home/codio/workspace/conf/wsgi.py # <-- this is where I put my wsgi.py file               
WSGIPythonPath /home/codio/workspace                         
                          
                                             
Order deny,allow                                           
Require all granted                                       
                                                  

Then start apache, and visit your site at http://UNIQUE-NAME.codio.io:3000/:
parts start apache2

You will probably see a ‘500 Internal Server Error’ message. So tail the apache ErrorLog file in the terminal to see what went wrong:
tail -f /home/codio/.parts/var/apache2/log/error_log

Here’s what I see (and I haven’t figured the fix yet):

[] mod_wsgi (pid=912): Target WSGI script '/home/codio/workspace/conf/wsgi.py' cannot be loaded as Python module.                                                                  
[] mod_wsgi (pid=912): Exception occurred processing WSGI script '/home/codio/workspace/conf/wsgi.py'.                                                                             
[] Traceback (most recent call last):                                                  
[]   File "/home/codio/workspace/conf/wsgi.py", line 5, in                     
[]     from django.core.wsgi import get_wsgi_application                               
[]   File "/home/codio/.parts/packages/python2/2.7.6/lib/python2.7/site-packages/django/core/wsgi.py", line 1, in                                                          
[]     from django.core.handlers.wsgi import WSGIHandler                               
[]   File "/home/codio/.parts/packages/python2/2.7.6/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 6, in                                                 
[]     from io import BytesIO                                                          
[]   File "/home/codio/.parts/packages/python2/2.7.6/lib/python2.7/io.py", line 51, in                                                                                     
[]     import _io                                                                      
[] ImportError: /home/codio/.parts/packages/python2/2.7.6/lib/python2.7/lib-dynload/_io.so: undefined symbol: PyUnicodeUCS2_Replace 

to be continued…
… issue fixed by installing python using the ‘parts’ command above

Similar Posts:

Tagged , ,

Django-TimelineJS Pluggable App

I like VeriteCo’s TimelineJS. It’s elegant, easy to use and very useful. I don’t like editing Google a google spreadsheet every time I want to update the timeline although it might be easier for some people. So, I wrote a small pluggable app for django to embed a timeline in django templates.

The code is loosely based on the code for the WordPress plugin. And I’ll try to copy over more code and fixes from that soon.

For now, it’s a working app with an admin backend to enter the timeline information and events, and to edit the options. It’s also a custom template tag that you can use to embed the timeline in your own templates.

Should be easy enough to use it.

pip install django-timelinejs

then include it in your INSTALLED_APPS and ./manage.py syncdb

Drop me a note if you find a bug or want some help. You can use the issues in GitHub for that.

Similar Posts:

Tagged , , , ,

Django Class-Based Generic Views

After working with Paypouy on a quick project using the php-based, shared-hosting-friendly CodeIgniter, I’m back to the more familiar waters of Python/Django.

I was asked to create a small organizer/calendar type app for a client to be used mainly from a smart phone. The app seemed very straight forward, so I thought I’d rely on the generic views for most of the work.

Check out some of the code after the break Continue reading

Similar Posts:

Tagged , , , , ,

Django uWSGI and Nginx on Ubuntu 11.10

I’m working my way through installing my simple django app on a new Ubuntu machine using the components mentioned above.

So far, here’s what I’ve done:

  • apt-get install nginx-full
  • apt-get install uwsgi uwsgi-plugin-python
  • installed django from svn described in the docs

The nginx configuration can be found in /etc/nginx/sites-available/default. Of course, it’s best to make a copy of that file and edit it for the site/app I’m working on. Here’s what I put in it:

server {
    listen   80;
    server_name sss.grat.in;
    access_log /var/log/nginx/sss.grat.in-access.log;
    location /media {
        alias /home/sss/app/media/;
    }
    location /static {
        alias /home/sss/app/static/;
    }
    location / {
        uwsgi_pass unix:///var/run/sss.grat.in.sock;
        include uwsgi_params;
    }
}

Now for the uWSGI configuration file. I created the file /etc/uwsgi/apps-available/sss.ini and added the following lines:

[uwsgi]
chdir = /home/sss/app
pythonpath = /home/sss
env = DJANGO_SETTINGS_MODULE=app.settings
# load django
module = django.core.handlers.wsgi:WSGIHandler()

it’s really just that. What took me so long was to figure out that in Debian/Ubuntu you have to install uwsgi-plugin-python, otherwise you’ll keep on getting weird error messages. Also, the default ini file has most of the settings (in /etc/default/uwsgi) so you should either read or edit that to make sure you’re getting all the right values.

Similar Posts:

Tagged , , , ,

Site ScreenShot Part 2 – Planning

I was having a little conversation with my friend Ghassan from LebanonDesign and he mentioned his company was actually using something similar to SiteScreenShot to generate thumbnails for their different websites. So I thought I could scale my code to do the same. Of course, plenty of changes should be considered:

  • Snapshot generation currently takes 3-5 seconds to complete. So I need to do 2 things:
    • Tell the user that the app is actually working on generating the snapshot
    • Provide a way to retrieve the pic once the snapshot is generated
    • Make things go faster if possible
  • Getting more users means more load on the server, what should we use?
    • Replace Apache with Nginx for faster static file delivery
    • Use a queue manager like Celery/RabbitQM to distribute the load

I’m currently testing some code with django-celery and I’ll try to post more information here (so I can remember what I’m doing)

Also on my list is making the site accessible via a simple API.

I also need to research a better way to take the screenshots on a headless server.

[adsenseyu3]

Similar Posts:

Tagged , , , , , ,

Site Screen Shot

Last week my friend Fernando Duran posted a note about his Site2Pic weekend app. It sounded like a great idea, so I asked Paypouy to cook up a simple design and wrote my own version using django and a few other open source libraries.

[adsenseyu2]

My app now lives at SSS.GRAT.IN

Take it for a spin, and tweet me @abdallahdeeb if you like it 🙂

Similar Posts:

Tagged , , ,