0% found this document useful (0 votes)
67 views7 pages

LAB # 13 Introduction and Installation of Django: Virtual Environment

1) The document discusses setting up Django by creating a virtual environment, installing Django and its dependencies, and generating a basic Django project structure. 2) Key steps include using virtualenv to isolate the Python environment, installing Django via pip, configuring the Django project settings file, and migrating the database. 3) The last section covers starting the Django development web server to test that the new project is set up correctly.

Uploaded by

Abdul Raouf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views7 pages

LAB # 13 Introduction and Installation of Django: Virtual Environment

1) The document discusses setting up Django by creating a virtual environment, installing Django and its dependencies, and generating a basic Django project structure. 2) Key steps include using virtualenv to isolate the Python environment, installing Django via pip, configuring the Django project settings file, and migrating the database. 3) The last section covers starting the Django development web server to test that the new project is set up correctly.

Uploaded by

Abdul Raouf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Web Engineering

LAB # 13

Introduction and Installation of Django


Objective:
To learn
 Basics of Django
 How it is installed
 How to create a project in Django

Introduction

Set up virtualenv and install Django

Virtual environment
Before we install Django we will get you to install an extremely useful tool to help keep your
coding environment tidy on your computer. It's possible to skip this step, but it's highly
recommended. Starting with the best possible setup will save you a lot of trouble in the
future!

So, let's create a virtual environment (also called a virtualenv). Virtualenv will isolate your
Python/Django setup on a per-project basis. This means that any changes you make to one
website won't affect any others you're also developing. Neat, right?

All you need to do is find a directory in which you want to create the virtualenv; your home
directory, for example. On Windows, it might look like C:\Users\Name\ (where Name is the
name of your login).

For this tutorial we will be using a new directory djangoprojects from your home directory:

command-line

> cd djangoprojects
We will make a virtualenv called myvenv. The general command will be in the format:

To create a new virtualenv, you need to open the command prompt and run python -m venv
myvenv. It will look like this:
command-line

118
Web Engineering

C:\djangoproejcts> python -m venv myvenv

Where myvenv is the name of your virtualenv. You can use any other name, but stick to
lowercase and use no spaces, accents or special characters. It is also good idea to keep the
name short – you'll be referencing it a lot!

Working with virtualenv


The command above will create a directory called myvenv (or whatever name you chose) that
contains our virtual environment (basically a bunch of directory and files).

Start your virtual environment by running:

command-line

C:\djangoprojects> myvenv\Scripts\activate

You will know that you have virtualenv started when you see that the prompt in your console
is prefixed with (myvenv).

When working within a virtual environment, python will automatically refer to the correct
version so you can use python instead of python3.

OK, we have all important dependencies in place. We can finally install Django!

Installing Django
Now that you have your virtualenv started, you can install Django.
Before we do that, we should make sure we have the latest version of pip, the software that
we use to install Django:
command-line

(myvenv) ~$ python -m pip install --upgrade pip

Installing packages with requirements


A requirements file keeps a list of dependencies to be installed using pip install:
First create a requirements.txt file inside of the djangoprojects/ folder, using the code editor that
you installed earlier. You do this by opening a new file in the code editor and then saving it
as requirements.txt in the djangoprojects/ folder. Your directory will look like this:

119
Web Engineering

djangoprojects
└───requirements.txt

In your djangoprojects/requirements.txt file you should add the following text:


djangogirls/requirements.txt

Django~=2.0.6

Now, run pip install -r requirements.txt to install Django.

command-line
(myvenv) ~$ pip install -r requirements.txt
Collecting Django~=2.0.6 (from -r requirements.txt (line 1))
Downloading Django-2.0.6-py3-none-any.whl (7.1MB)
Installing collected packages: Django
Successfully installed Django-2.0.6

That's it! You're now (finally) ready to create a Django application!

Your first Django project!


We're going to create a small blog!

The first step is to start a new Django project. Basically, this means that we'll run some
scripts provided by Django that will create the skeleton of a Django project for us. This is just
a bunch of directories and files that we will use later.

The names of some files and directories are very important for Django. You should not
rename the files that we are about to create. Moving them to a different place is also not a
good idea. Django needs to maintain a certain structure to be able to find important things.

On Windows you should run the following command. (Don't forget to add the period (or
dot) . at the end):
command-line

(myvenv) C:\Users\Name\ djangoprojects> django-admin.exe startproject mysite .

django-admin.pyis a script that will create the directories and files for you. You should now
have a directory structure which looks like this:
djangoprojects
├───manage.py
├───mysite
│ settings.py
│ urls.py

120
Web Engineering

│ wsgi.py
│ __init__.py
└───requirements.txt
Note: in your directory structure, you will also see your venv directory that we created
before.

manage.py is a script that helps with management of the site. With it we will be able (amongst
other things) to start a web server on our computer without installing anything else.

The settings.py file contains the configuration of your website.


Remember when we talked about a mail carrier checking where to deliver a letter? urls.py file
contains a list of patterns used by urlresolver.

Let's ignore the other files for now as we won't change them. The only thing to remember is
not to delete them by accident!

Changing settings
Let's make some changes in mysite/settings.py. Open the file using the code editor you
installed earlier.

Note: Keep in mind that settings.py is a regular file, like any other. You can open it from inside
the code editor, using the "file -> open" menu actions. This should get you the usual window
in which you can navigate to your settings.py file and select it. Alternatively, you can open the
file by navigating to the djangoprojects folder on your desktop and right-clicking on it. Then,
select your code editor from the list. Selecting the editor is important as you might have other
programs installed that can open the file but will not let you edit it.

It would be nice to have the correct time on our website. Go to Wikipedia's list of time zones
and copy your relevant time zone (TZ) (e.g. Europe/Berlin).

In settings.py, find the line that contains TIME_ZONE and modify it to choose your own
timezone. For example:

mysite/settings.py

TIME_ZONE = 'Europe/Berlin'

A language code consist of the language, e.g. en for English or de for German, and the country
code, e.g. de for Germany or ch for Switzerland. If English is not your native language, you can
add this to change the default buttons and notifications from Django to be in your language.

121
Web Engineering

So you would have "Cancel" button translated into the language you defined here. Django
comes with a lot of prepared translations.

If you want a different language, change the language code by changing the following line:

mysite/settings.py

LANGUAGE_CODE = 'de-ch'

We'll also need to add a path for static files. (We'll find out all about static files and CSS later
in the tutorial.) Go down to the end of the file, and just underneath the STATIC_URL entry, add
a new one called STATIC_ROOT:

mysite/settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['localhost',
'127.0.0.1', '[::1]']. This won't match our hostname on PythonAnywhere once we deploy our
application so we will change the following setting:

mysite/settings.py

ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']

Set up a database
There's a lot of different database software that can store data for your site. We'll use the
mysql.
To set up this go to your mysite/settings.py file:

mysite/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': ‘mydjangoproject’,
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}

122
Web Engineering

To create a database for our blog, let's run the following in the console: python manage.py
(we need to be in the djangoprojects directory that contains the manage.py file). If that
migrate
goes well, you should see something like this:
pip install mysql-python -apt-get install python3-mysqldb libmysqlclient-dev python-dev

command-line
(myvenv) ~/djangoprojects> python manage.py migrate
Operations to perform:
Apply all migrations: auth, admin, contenttypes, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

And we're done! Time to start the web server and see if our website is working!

Starting the web server


You need to be in the directory that contains the manage.py file (the djangoprojects directory).
In the console, we can start the web server by running python manage.py runserver:

command-line
(myvenv) ~/djangoprojects python manage.py runserver

If you are on Windows and this fails with UnicodeDecodeError, use this command instead:

command-line
(myvenv) ~/djangoprojects$ python manage.py runserver 0:8000

123
Web Engineering

Now you need to check that your website is running. Open your browser (Firefox, Chrome,
Safari, Internet Explorer or whatever you use) and enter this address:

browser
http://127.0.0.1:8000/

Congratulations! You've just created your first website and run it using a web server! Isn't
that awesome?

LAB TASK

1. Install and configure Django


2. Create a project structure in Django

124

You might also like