Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Sunday, September 14, 2014

Modifying the DigitalOcean django one-click project to use a custom directory

Hi all,

I recently gave DigitalOcean a shot on the recommendation of someone at HackerNews. DigitalOcean has a lot of custom droplets, which are basically Virtual Machines, that you can use to set up a cloud server on which to host your website.

I had been building a Django website that I wanted to put up, and while setting it up on DigitalOcean, I ran into the following problem: The default Django droplet uses a base Django project located in the 'home' directory of the UNIX installation DigitalOcean sets up for you. I wanted it to point to a custom directory in the /opt folder that I had set up.

In order to do this, remote into the DigitalOcean droplet. Once there, run the following command:

    sudo nano /etc/init/gunicorn.conf

In this file, change 'chdir /home/django' to point to the directory that contains your django project. In my case, I changed it to '/opt/venv'.

Change the 'name' field to refer to the folder that contains your 'wsgi' file. In my case, I changed it to 'name=jobs'.

Change the 'pythonpath' field to refer to folder that contains your 'manage.py' file. In my case, I changed it to 'pythonpath=search-jobs'.

In 'django_project.wsgi:application' change 'django_project' to the name of your Django app (ie. what you changed your 'name' field to).

Now run the following command:

    sudo nano /etc/nginx/sites-enabled/django

In this file, change the media and static locations to point to where you need them to point to. In my case I changed:

    alias /home/django/django_project/django_project/media;

to:

    alias /opt/venv/search-jobs/media;

and:

    alias /home/django/django_project/django_project/static;

to:

    alias /opt/venv/search-federal-jobs/staticfiles;


That should set up the default nginx and gunicorn to serve your django project located in your custom path properly!

If you have any feedback on this post, or if it helped, drop me a comment and let me know.

Sunday, December 23, 2012

Django and sqlite3

This is a minor issue that bothered me for a while that I don't think the documentation makes clear.

When I was trying to set up an sqlite3 database with my django install, I was very confused because the tutorial I followed told me to enter in my settings.py file the location of the database. But I didn't have any database installed! Searching the internet returned several allusions to the fact that python2.5+ came with sqlite3 so I didn't have to install or download anything ... but if I didn't, how was I going to find where this elusive sqlite3 db was so that I could link django to it?

It turns out that django creates the database for you! So what you want to enter in the settings.py file is the link to the db file as you want it to be; for example: '/home/khalid/myproj/proj.db'

ProTip:

Consider defining the db url like so:

'NAME': os.path.join(os.path.dirname(__file__), '../proj.db').replace('\\','/')

This will make it so that the new db file is one directory above your settings.py file. If you want to do this, make sure you import settings.path at the top of your file.

Final Code:

import settings.path

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': os.path.join(os.path.dirname(__file__), '../proj.db').replace('\\','/'),  # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}


Cheerio.