"How do I setup a Django Project from scratch?"
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
This tutorial answers the question, "How do I setup a Django Project from scratch?". Since you're reading this, I assume (err, hope) you know that Django is a Python web framework built for rapid web development. We'll go through the setup, detailing the basic installation procedures of Django and the dependencies required as well as a few additional libraries/extensions to get you started developing ASAP.
We'll also look at a basic workflow you can use as soon as your project structure is setup.
I recommend that you follow these steps in order the first time you start a new Django Project. Each step is modular, so feel free to jump around after the initial setup
Regardless of the Django version, please follow this setup process.
You should have some Python experience and know basic Unix bash commands. If you've never used the command line before, please familiarize yourself with the following commands: pwd, cd, ls, rm, and mkdir.
For simplicity, all examples use the Unix-style prompt:
$ python manage.py runserver
(The dollar sign is not part of the command.)
C:\> python manage.py runserver
Also, in a Unix environment, you may need to add
sudo to each command:
$ sudo python manage.py runserver
Sudo simply allows regular users to run commands with admin privileges, which modifies the system for all users. You will need to enter your admin password when using sudo.
This basic setup will go through the first six steps since they do not depend on your Django version; the setup process will continue after you decide which version you'd like to utilize.
Note: This tutorial utilizes Python version 2.7.7; however, Python 3 will work as well.
Unix environments come pre-installed with Python. To check your Python version, run the command:
$ python -V Python 2.7.7
If you already have a 2.7.x version, move on to the next step. If not, download and install the latest 2.7.x or 3.x version
Both easy_install and pip are Python Package Managers, which make it much easier to install and upgrade Python packages (and package dependencies).
To download easy_install, go to the Python Package Index (PyPI). You need to download setuptools, which includes easy_install. Download the package egg (.egg), then install it directly from the file.
Pip, meanwhile, is a wrapper that relies on easy_install, so you must have easy_install setup and working first before you can install pip. Once easy_install is setup, run the following command to install pip:
$ easy_install pip
For version control, we'll be using Git. You can check your current version, if you have git already installed, with the following command:
$ git --version git version 1.9.0
If you do not have a version greater than 1.7.x installed, please download the latest version.
SQLite comes pre-installed with Python, and most tutorials utilize SQLite - so let's push ourselves a bit and use MySQL instead.
First, install MySQL from here.
Next, start the server, and then setup a new database and a user:
$ mysql.server start $ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> mysql> CREATE DATABASE django_db; Query OK, 1 row affected (0.01 sec) mysql> mysql> quit Bye
It's common practice to use a virtualenv (virtual environment) for your Python projects, which is used to create self-contained development environments (also called "sandboxes").
Think of virtualenv as a completely isolated container within your computer, where you can install any version of Python and/or libraries and it won't affect anything outside that container. It's like an isolated, soundproof room within your home where you can scream as loud as you want, about anything you want, and nobody else outside that room can hear it.
NOTE: Besides creating isolated development enviornments, I also use virtualenv to experiment with other versions of Python. I do not want to upgrade my main version of Python, so I have isolated "containers" with specific versions of Python in them to experiment with.
Install virtualenv with the following command:
$ pip install virtualenv
Set up your development structure:
$ mkdir django_project $ cd django_project $ virtualenv --no-site-packages env $ source env/bin/activate
You should see (env) before your prompt,
(env)$, indicating that you're running within the 'env' virtualenv.
To exit the virtualenv, type the following command:
Then reactivate when you're ready to work again.
Make sure you have some sort of code editor. When in doubt, you cannot go wrong with Submite Text.
That's it for the basic setup. Take a look at the overall workflow before moving on to the actual Django project setup.