Start working with django


What do you need?



  1. Python installed on your pc
  2. Virtualenv wrapper for virtual enviroment when working with projects
  3. Django installed



Before you can start learning Django, you must install some software on your computer. Fortunately, this is a simple three step process:
  1. Install Python;
  2. Install a Python Virtual Environment; and
  3. Install Django.
For Windows users, your computer can be running any recent version of Windows (7, 8.1 or 10).

Installing Python on your PC visit their docs

Creating a Python Virtual Environment

When you are writing new software programs, it’s possible (and common!) to modify dependencies and environment variables that your other software depends on. This can cause numerous problems, so should be avoided. A Python virtual environment solves this problem by wrapping all the dependencies and environment variables that your new software needs into a file system separate from the rest of the software on your computer.
The virtual environment tool in Python is called venv, but before we set up venv, we need to create our club site project directory.

Creating a Project Directory

Before we create our virtual environment, we first need to create a project folder that will house not only our virtual environment, but all the code and media for our Django club site.
The project folder can go anywhere on your computer, although it’s highly recommended that it be created somewhere in your user directory, so you don’t get permission issues later on. A good place for your project in Windows is your My Documents folder. On a Mac your Documents folder is also a logical choice, however it can go anywhere in your user directory.
Create a new folder on your system. I have named the folder myclub_project, but you can give the folder any name that makes sense to you.
For the next step you need to be in a command window (terminal on Linux and macOS). The easiest way to do this in Windows is to open Windows Explorer, hold the SHIFT key and right-click the folder to get the context menu and click on Open command window here (Figure 2-5). On a Mac, CTRL-click the folder and select New Terminal at Folder.
Figure 2.5: Hold the shift key and right-click a folder to open a command window.


Create a Python Virtual Environment

Once you have created your project folder, you need to create a virtual environment for your project by typing the following at the command prompt you just opened:
On Windows
PS ...\Documents\myclub_project> python -m venv env_myclub
On Mac
...$ python3 -m venv env_myclub
The function of this command is straight forward—the -m option tells Python to run the venv module as a script. venv in turn requires one parameter: the name of the virtual environment to be created. So this command is saying “create a new Python virtual environment and call it env_myclub
Once venv has finished setting up your new virtual environment, it will switch to an empty command prompt. When it’s done, open Windows Explorer and have a look at what venv created for you. In your project folder you will now see a folder called \env_myclub (or whatever name you gave the virtual environment). If you open the folder on Windows, you will see the following:
\Include 
\Lib 
\Scripts
pyvenv.cfg  
On a Mac, it’s:
/bin
/Include 
/Lib
pyvenv.cfg  
On either platform, if you look inside the \Lib folder, you will see venv has created a complete Python installation for you, separate from your other software, so you can work on your project without affecting any of the other software on your system.
To use this new Python virtual environment we have to activate it, so let’s go back to the command prompt and type the following:
On Windows
env_myclub\scripts\activate
On Mac
...$ source env_myclub/bin/activate 
This will run the activate script inside your virtual environment’s \scripts folder. You will notice your command prompt has now changed:
(env_myclub) PS ...\Documents\myclub>
On a Mac, the prompt looks like this:
(env_myclub) ... <yourusername>$
The (env_myclub) at the beginning of the command prompt lets you know that you are running in the virtual environment. Our next step is to install Django.

Installing Django

Now that we have Python installed and are running a virtual environment, installing Django is super easy, just type the command:
pip install "django>=2.2,<3"
If you are not familiar with the pip command, put briefly it’s the Python package manager and is used to install Python packages (To keep with good programming tradition, pip is a recursive acronym for “Pip Installs Packages”).
This will instruct pip to install the latest version of Django 2.2 into your virtual environment. Your command output should look like this:
(env_myclub) ...> pip install "django>=2.2,<3"
Collecting django<3,>=2.2
Downloading .../Django-2.2.1-py3-none-any.whl (7.4MB)
    100% |################################| 7.5MB 3.3MB/s
Collecting sqlparse (from django<3,>=2.2)
Using cached https:.../sqlparse-0.3.0-py2.py3-none-any.whl
Collecting pytz (from django<3,>=2.2)
Downloading .../pytz-2019.1-py2.py3-none-any.whl (510kB)
    100% |################################| 512kB 3.4MB/s
Installing collected packages: sqlparse, pytz, django
Successfully installed django-2.2.1 pytz-2019.1 sqlparse-0.3.0
To test the installation, go to your virtual environment command prompt, start the Python interactive interpreter by typing python and hitting Enter. If the installation was successful, you should be able to import the module django:
(env_myclub) ...\myclub_project>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'2.2.1'
>>> exit()
Don’t forget to exit the Python interpreter when you are done.
You can also check if Django has been installed directly from the command prompt with:
(env_myclub) ...\myclub_project>python -m django --version
2.2.1
Chapter Summary
In this chapter, I showed you how to install Python 3 and Django on both Windows and macOS. In the next chapter, we’re going to step back a bit and have a big picture look at how Django is structured and how all the parts of Django work together to create powerful, scalable web applications.

Post a Comment

0 Comments