Python-Django/C2/Getting-started-with-Django/English

From Script | Spoken-Tutorial
Revision as of 06:04, 8 November 2019 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Getting Started With Django

Keywords: Python Django, Video tutorial, install Django, Python virtual environment, pip, start Django Project, Django Project file structure

Visual cue Narration
Slide: Getting started with Django Hello and welcome to the spoken tutorial on “Getting started with Django
Slide: Learning Objectives In this tutorial, we will learn
  • About Python Virtual Environment
  • How to use the Virtual Environment
  • How to install Django
  • How to start a Django project and
  • The Django project file structure
Slide:System Requirements To record this tutorial, I am using
  • Ubuntu Linux 16.04
  • Python 3.5
  • Python 3.4-venv or higher and
  • Firefox web browser.

However you may use any other browser of your choice.

Slide:Python Virtual Environment
  • Python virtual environment is an isolated working copy of Python
  • virtualenv is a tool to create an isolated Python environment
  • It allows us to work on a specific Django project without affecting other projects
  • It does not require root access
Slide: pip
  • One can install, upgrade and remove Python packages using a program called pip
  • By default, pip will install packages from the Python Package Index https://pypi.python.org/pypi
Open a terminal window

Press CTRl+ALT+T

Open the Terminal by pressing Ctrl, Alt and T keys together.
[Terminal] Let me create a folder named my-django.


This folder will help me to organize my files in a single location on my machine.

[Terminal]

Type mkdir my-django

Press Enter

To do so, type mkdir <space> my <hyphen> django and press Enter.
[Terminal]

Type cd my-django and press Enter

Then type cd <space> my hyphen django and press Enter.
[Terminal]


Type python3 -m venv myapp_env

Let us now create a new virtual environment named myapp underscore env.


Type python three <space> hyphen m <space> venv <space> myapp underscore env

[Terminal]

Highlight -m


Highlight venv

hyphen m option is used to pass the python module as a command line argument.


Here venv is the python module.


The given module is executed as a script.

Press Enter Now press Enter.
[Terminal]

Type ls

Type ls and press Enter.


We can see that a directory named myapp underscore env has been created.

[Terminal]

Type ls myapp_env and press Enter

Type ls <space> myapp underscore env and press Enter.
[Terminal]

Point to the list of directories in the terminal

We see that the virtual environment contains various directories.
[Terminal]

Point to bin

bin contains executable files.
[Terminal]

Point to lib


ls <YOUR_PATH TO site-packages>

lib contains supporting library files.

Packages installed in this environment will be in site-packages.

[Terminal]

Type:

source myapp_env/bin/activate

and press Enter

Let us now activate the virtual environment:

Type source <space> myapp underscore env slash bin slash activate

And press Enter.

[Terminal]

Highlight

(myapp_env)

So, the virtual environment is active now.

We can see an additional text in the command prompt.

It shows the virtual environment we are using - that is myapp underscore env.

[Terminal]

Type python -m django --version and press Enter

Now, let's learn how to install Django.

First, let’s check whether Django is already installed on our system or not.

To do so type python <space> hyphen m <space> django <space> hyphen hyphen ­version

And press Enter.

[Terminal]

Point to the error message

Obviously it is not installed in this Virtual Environment.

So we will see an error message No module named django.

[Terminal]

Type pip install django==2.0 and press Enter

In the new environment, we will use pip to install Django.

First let me clear the terminal by pressing Ctrl and L keys

Type pip <space> install <space> django <equal to equal to>2.0

We will use Django version 2.0

If we don’t mention <equal to equal to> 2.0 then the latest stable Django version will be installed.

Press Enter.

The installation may take some time depends upon your internet speed.

[Terminal]

Type python -m django --version and press Enter

Now, let us check whether the installation has been successful.

Clear the terminal.

Type python <space> hyphen m <space> django <space> hyphen hyphen version

And press Enter.

[Terminal]

Point to the output

The terminal output displays Django version 2.0 here.

This indicates that the Django package has been installed.

[Terminal]

Using command:

pip install -U django

We can also update the Django version in the future using the command:

pip <space> install <space> hyphen U <space> django

[Terminal]

Type pip --help or pip --help and press Enter

For more help on pip:

Type pip <space> hyphen hyphen help

And press Enter.

[Terminal]

Type django-admin startproject mysite and press Enter

Next we will create a Django project named mysite.

In the terminal, type:

django hyphen admin <space> startproject <space> mysite

And press Enter.

[Terminal]

Type ls and press Enter

Type ls and press Enter.

We can see the project directory mysite.

Let us now see the project file structure.

Switch to Slide

Slide: What is a Django project?

Before that we will understand what a Django project is.

A Django project is a collection of

  • Settings for an instance of Django
  • Database configuration
  • Django specific options and
  • Application specific settings
Slide: File structure

mysite/

manage.py

mysite/

  _init_.py
  settings.py
  urls.py
  wsgi.py 
The startproject command creates a project consisting of a file structure as shown here.
[Terminal] Switch back to the terminal.
[Terminal]

Type cd mysite

and press Enter

Now type cd <space> mysite and press Enter.

We are inside the mysite directory.

[Terminal]

Type ls and press Enter

Type ls and press Enter to list the contents.
Highlight the mysite file structure in the terminal Here we can see that the mysite file structure has been created.
[Terminal]

point to ~/myproject/mysite$

The outer mysite is just a container of our project.
[Terminal]

Point to manage.py

manage.py is a command line utility to interact with our Django project.
[Terminal]

Point to inner mysite

The inner mysite is the actual Python package for our project.
[Terminal]

Type ls mysite and press Enter

Now, type ls space mysite and press Enter.
[Terminal]

Point to __init__.py

We see init dot py

That means the mysite directory contains python packages.

[Terminal]

Point to settings.py

settings.py file contains all the configuration of our Django project.
[Terminal]

Point to urls.py

urls.py contains URL declarations for this project.
[Terminal]

Point to wsgi.py

wsgi.py is an entry-point for web servers to serve our project.
[Terminal]

Run the project

Next, let's see how to start the Django development server.
[Terminal]

Type python manage.py runserver and press Enter

Type python <space> manage dot py <space> runserver

And press Enter.

[Terminal]

Point to shell prompt

We can see that the server is running.
Open a browser and type http://localhost:8000/ Next, open a web browser.

In the address bar, type http://localhost:8000/ or http://127.0.0.1:8000/

And press Enter.

Point to the Django index page In the web browser, we see the Django index page.
Switch to Terminal Let us go back to the terminal.
Press Ctrl+c simultaneously To stop the server, press Ctrl and C keys simultaneously.
[Terminal]

Type deactivate and press Enter

When the virtual environment is not in use, we can deactivate it.

To do so, type deactivate and press Enter.

[Terminal]

Point the shell prompt

We can see that, the virtual environment name disappears from the command prompt.
Switch to Slide

Slide: Delete the virtual environment

If the virtual environment is no longer needed, we can delete it.

This is done by deleting its directory.

The command is rm <space> hyphen rf <space> myapp underscore env

Only narration For now, we will not delete.

We will keep the virtual environment for creating the Django project in the upcoming tutorials.

With this, we come to the end of this tutorial.

Let us summarize.

Slide:Summary In this tutorial, we have learnt -
  • About Python Virtual Environment
  • How to use the Virtual Environment
  • How to install Django
  • How to start a Django project and
  • The Django project file structure
Slide: Assignment As an assignment
  • Create a new virtual environment
  • Activate it
  • Install the latest version of Django
  • Create a new project named demo_project under the same virtual environment
Slide: About Spoken Tutorial project The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Slide:Spoken Tutorial workshops The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Slide: Forum for specific questions: Do you have questions in this Spoken Tutorial?

Please visit this site.

Choose the minute and second where you have the question.

Explain your question briefly.

Someone from our team will answer them.

Slide: Forum for specific questions: The Spoken Tutorial forum is for specific questions on this tutorial.

Please do not post unrelated and general questions on them.

This will help reduce the clutter.

With less clutter, we can use these discussion as instructional material.

Slide: FOSSEE to answer questions Do you have questions not related to the Spoken Tutorial?

Do you have general or technical questions on the software?

Please visit the FOSSEE Forum.

Choose the Software and post your question.

Slide:Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

Slide: Thanks Slide This script has been contributed by Thiagarajar College of Engineering and the FOSSEE Project, IIT Bombay.

The video has been recorded by Praveen from Spoken Tutorial Project, IIT Bombay.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Pravin1389