Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SQLite as default database #57

Merged
merged 5 commits into from
Jul 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
* [Domain](domain/README.md)
* [Homework: add more to your website!](homework/README.md)
* [Contributing and editing](contributing_and_editing_this_book/README.md)
* [Optional: PostgreSQL installation!](optional_postgresql_installation/README.md)

72 changes: 1 addition & 71 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,4 @@

Most web applications have their own database. A database is a collection of data. This is a place in which you will store information about users, your blog posts, etc.

We will be using a PostgreSQL database to store our data. It would be easier to use the default Django database adapter - called *sqlite* - but it's not good for production use. *Production* means out on the big, scary internet - running real websites accessed by more than a handful of users. If you want your application to be available to the world, not just running on your computer, you'll need PostgreSQL.

# PostgreSQL installation

## Windows

The easiest way to install Postgres on Windows is using a program you can find here: http://www.enterprisedb.com/products-services-training/pgdownload#windows

Choose the newest version available for your operating system. Download the installer, run it and then follow the instructions available here: http://www.postgresqltutorial.com/install-postgresql/. Take note of the installation directory as you will need it in the next step (typically, it's `C:\Program Files\PostgreSQL\9.3`).

## Mac OS X

The easiest way is to download the free [Postgres.app](http://postgresapp.com/) and install it like any other application on your operating system.

Download it, drag to the Applications folder and run by double clicking. That's it!

You'll also have to add the Postgres command line tools to your `PATH` variable, what is described [here](http://postgresapp.com/documentation/cli-tools.html).

## Linux

Installation steps vary from distribution to distribution. Below are the commands for Ubuntu and Fedora, but if you're using a different distro [take a look at the PostgreSQL documentation](https://wiki.postgresql.org/wiki/Detailed_installation_guides#General_Linux).

### Ubuntu

Run the following command:

sudo apt-get install postgresql postgresql-contrib

### Fedora

Run the following command:

sudo yum install postgresql93-server

# Create database

Next up, we need to create our first database, and a user that can access that database. PostgreSQL lets you create as many databases and users as you like, so if you're running more than one site you should create a database for each one.

## Windows

If you're using Windows, there's a couple more steps we need to complete. For now it's not important for you to understand the configuration we're doing here, but feel free to ask your coach if you're curious as to what's going on.

1. Open the Command Prompt (Start menu → All Programs → Accessories → Command Prompt)
2. Run the following by typing it in and hitting return: `setx PATH "%PATH%;C:\Program Files\PostgreSQL\9.3\bin"`. You can paste things into the Command Prompt by right clicking and selecting `Paste`. Make sure that the path is the same one you noted during installation with `\bin` added at the end. You should see the message `SUCCESS: Specified value was saved.`.
3. Close and then reopen the Command Prompt.

## Create the database

First, let's launch the Postgres console by running `psql`. Remember how to launch the console?
>On Mac OS X you can do this by launching the `Terminal` application (it's in Applications → Utilities). On Linux, it's probably under Applications → Accessories → Terminal. On Windows you need to go to Start menu → All Programs → Accessories → Command Prompt. Furthermore, on Windows, `psql` might require logging in using the username and password you chose during installation. If `psql` is asking you for a password and doesn't seem to work, try `psql -U <username> -W` first and enter the password later.

$ psql
psql (9.3.4)
Type "help" for help.
#

Our `$` now changed into `#`, which means that we're now sending commands to PostgreSQL. Let's create a user:

# CREATE USER name;
CREATE ROLE

Replace `name` with your own name. You shouldn't use accented letters or whitespace (e.g. `bożena maria` is invalid - you need to convert it into `bozena_maria`).

Now it's time to create a database for your Django project:

# CREATE DATABASE djangogirls OWNER name;
CREATE DATABASE

Remember to replace `name` with the name you've chosen (e.g. `bozena_maria`).

Great - that's databases all sorted!
We will be using a SQLite database to store our data. This is the default Django database adapter - but it's not good for production use. *Production* means out on the big, scary internet - running real websites accessed by more than a handful of users. If you want your application to be available to the world, not just running on your computer, you'll need PostgreSQL.
66 changes: 24 additions & 42 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ As you learned, a website has to be located on a server. There are a lot of prov

We will be following this tutorial: https://devcenter.heroku.com/articles/getting-started-with-django, but we pasted it here so it's easier for you.

## Requirements.txt
## The `requirements.txt` file

We need to create a `requirements.txt` file to tell Heroku what Python packages need to be installed on our server.

But first, Heroku needs us to install the `django-toolbelt` package. Go to your console with `virtualenv` activated and type this:

(venv) $ pip install django-toolbelt
(venv) $ pip install dj-database-url gunicorn whitenoise

After the installation is finished, run this command:

(venv) $ pip freeze > requirements.txt

This will create a file called `requirements.txt` with a list of your installed packages (i.e. Python libraries that you are using, for example Django :)).

Open this file and add the following line at the bottom:

pyscopg2==2.5.3

This line is needed for your application to work on Heroku.


## Procfile

Another thing we need to create is a Procfile. Open up your code editor, create a file called `Procfile` in `mysite` directory and add this line:
Expand All @@ -28,7 +35,7 @@ Another thing we need to create is a Procfile. Open up your code editor, create

Then save it. Done!

## Runtime.txt
## The `runtime.txt` file

We need to tell Heroku which Python version we want to use. This is simply done by creating a `runtime.txt` and putting the following text inside:

Expand All @@ -40,17 +47,18 @@ There is a difference between settings we are using locally (on our computer) an

Go ahead and create `mysite/local_settings.py` file. It should contain your `DATABASE` setup from your `mysite/settings.py` file. Just like that:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangogirls',
'USER': 'yourname',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

DEBUG = True

Then just save it! :)

## mysite/settings.py
Expand All @@ -65,6 +73,8 @@ Another thing we need to do is modify our website's `settings.py` file. Open `my
ALLOWED_HOSTS = ['*']

STATIC_ROOT = 'staticfiles'

DEBUG = False

At the end of the `mysite/settings.py`, copy and paste this:

Expand All @@ -77,41 +87,12 @@ It'll import all of your local settings if the file exists.

Then save the file.

## mysite/urls.py

Open `mysite/urls.py` file and add these two lines in the beginning of the file:

from django.conf.urls.static import static
from django.conf import settings

And add this line after last `)`:

+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The whole thing should look like this:

from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

## mysite/wsgi.py

Open the `mysite/wsgi.py` file and replace this line:

application = get_wsgi_application()

with this:

from dj_static import Cling
application = Cling(get_wsgi_application())
Open the `mysite/wsgi.py` file and add these lines at the end:

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

All right!

Expand Down Expand Up @@ -141,6 +122,7 @@ Create `.gitignore` file with following content:
__pycache__
staticfiles
local_settings.py
db.sqlite3

and save it. The dot on the beginning of the file name is important! As you can see, we're now telling Heroku to ignore `local_settings.py` and don't download it, so it's only available on your computer (locally).

Expand Down
41 changes: 4 additions & 37 deletions django_installation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ For this tutorial we will be using a new directory `djangogirls` from your home

mkdir djangogirls

We will make a virtualenv called `venv`. The general command will be in the format python -m venv `name_of_venv`.
We will make a virtualenv called `venv`. The general command will be in the format:

python -m venv `name_of_venv`.

### Windows

Expand All @@ -40,7 +42,7 @@ Creating a `virtualenv` on both Linux and OS X is as simple as running:

The command above will create a folder called `venv` that contains our virtual environment (basically bunch of folders and files). All we want to do now is starting it by running:

C:\Users\Name> venv\Scripts\activate
C:\Users\Name\djangogirls> venv\Scripts\activate

on Windows, or:

Expand Down Expand Up @@ -74,39 +76,4 @@ Now that you have your `virtualenv` started, you can install Django using `pip`.

> If you get an error when calling pip on Ubuntu 12.04 please run `python -m pip install -U --force-reinstall pip` to fix the pip installation in the virtualenv.

### Installing PostgreSQL package for Python

First, install Heroku Toolbelt from https://toolbelt.heroku.com/ While we will need this mostly for deploying your site later on, it also includes Git, which might come in handy already.

Next up, we need to install a package which lets Python talk to PostgreSQL - this is called `psycopg2`. The installation instructions differ slightly between Windows and Linux/OS X.

### Windows

For Windows, download the pre-built file from http://www.stickpeople.com/projects/python/win-psycopg/

Make sure you get the one corresponding to your Python version (3.4 should be the last line) and to the correct architecture (32 bit in the left column or 64 bit in the right column).

Rename the downloaded file and move it so that it's now available at `C:\psycopg2.exe`.

Once that's done, enter the following command in the terminal (make sure your `virtualenv` is activated):

easy_install C:\psycopg2.exe

### Linux and OS X

Run the following in your console:

(venv) ~/djangogirls$ pip install psycopg2

If that goes well, you'll see something like this

Downloading/unpacking psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2
Cleaning up...

---

Once that's completed, run `python -c "import psycopg2"`. If you get no errors, everything's installed successfully.

That's it! You're now (finally) ready to create a Django application! But to do that, you need a nice program to write your code in...
4 changes: 2 additions & 2 deletions django_start_project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ It would be nice to have the correct time on our website. You should find lines

## Setup a database

There's a lot of different database software that can store data for your site. We'll use one of the best ones, `PostgreSQL`, which we installed in __Database__ chapter.
There's a lot of different database software that can store data for your site. We'll use the default one, `sqlite3`.

Find this part in your `mysite/settings.py` file:
This is already set up in this part of your `mysite/settings.py` file:

DATABASES = {
'default': {
Expand Down
Loading