Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Django 2 by Example

You're reading from  Django 2 by Example

Product type Book
Published in May 2018
Publisher Packt
ISBN-13 9781788472487
Pages 526 pages
Edition 1st Edition
Languages
Author (1):
Antonio Melé Antonio Melé
Profile icon Antonio Melé
Toc

Table of Contents (15) Chapters close

Preface 1. Building a Blog Application 2. Enhancing Your Blog with Advanced Features 3. Extending Your Blog Application 4. Building a Social Website 5. Sharing Content in Your Website 6. Tracking User Actions 7. Building an Online Shop 8. Managing Payments and Orders 9. Extending Your Shop 10. Building an E-Learning Platform 11. Rendering and Caching Content 12. Building an API 13. Going Live 14. Other Books You May Enjoy

Installing Django

If you have already installed Django, you can skip this section and jump directly to the Creating your first project section. Django comes as a Python package and thus can be installed in any Python environment. If you haven't installed Django yet, the following is a quick guide to install Django for local development.

Django 2.0 requires Python version 3.4 or higher. In the examples for this book, we will use Python 3.6.5. If you're using Linux or macOS X, you probably have Python installed. If you are using Windows, you can download a Python installer at https://www.python.org/downloads/windows/.

If you are not sure whether Python is installed on your computer, you can verify it by typing python in the shell. If you see something like the following, then Python is installed on your computer:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

If your installed Python version is lower than 3.4, or if Python is not installed on your computer, download Python 3.6.5 from https://www.python.org/downloads/ and install it.

Since you will use Python 3, you don't have to install a database. This Python version comes with a built-in SQLite database. SQLite is a lightweight database that you can use with Django for development. If you plan to deploy your application in a production environment, you should use an advanced database, such as PostgreSQL, MySQL, or Oracle. You can get more information about how to get your database running with Django at https://docs.djangoproject.com/en/2.0/topics/install/#database-installation.

Creating an isolated Python environment

It is recommended that you use virtualenv to create isolated Python environments, so that you can use different package versions for different projects, which is far more practical than installing Python packages system-wide. Another advantage of using virtualenv is that you won't need any administration privileges to install Python packages. Run the following command in your shell to install virtualenv:

pip install virtualenv

After you install virtualenv, create an isolated environment with the following command:

virtualenv my_env

This will create a my_env/ directory, including your Python environment. Any Python libraries you install while your virtual environment is active will go into the my_env/lib/python3.6/site-packages directory.

If your system comes with Python 2.X and you have installed Python 3.X, you have to tell virtualenv to use the latter. 

You can locate the path where Python 3 is installed and use it to create the virtual environment with the following commands:

zenx$ which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
zenx$ virtualenv my_env -p /Library/Frameworks/Python.framework/Versions/3.6/bin/python3

Run the following command to activate your virtual environment:

source my_env/bin/activate

The shell prompt will include the name of the active virtual environment enclosed in parentheses, as follows:

(my_env)laptop:~ zenx$

You can deactivate your environment at any time with the deactivate command.

You can find more information about virtualenv at https://virtualenv.pypa.io/en/latest/.

On top of virtualenv, you can use virtualenvwrapper. This tool provides wrappers that make it easier to create and manage your virtual environments. You can download it from https://virtualenvwrapper.readthedocs.io/en/latest/.

Installing Django with pip

The pip package management system is the preferred method for installing Django. Python 3.6 comes with pip preinstalled, but you can find pip installation instructions at https://pip.pypa.io/en/stable/installing/.

Run the following command at the shell prompt to install Django with pip:

pip install Django==2.0.5

Django will be installed in the Python site-packages/ directory of your virtual environment.

Now, check whether Django has been successfully installed. Run python on a terminal, import Django, and check its version, as follows:

>>> import django
>>> django.get_version()
'2.0.5'

If you get the preceding output, Django has been successfully installed on your machine.

Django can be installed in several other ways. You can find a complete installation guide at https://docs.djangoproject.com/en/2.0/topics/install/.
You have been reading a chapter from
Django 2 by Example
Published in: May 2018 Publisher: Packt ISBN-13: 9781788472487
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}