Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
66 views

Django Python 1

This document provides an introduction and overview of Django and Python web development. It discusses installing Python, virtualenv, and Django to set up a development environment. It then explains how to start a new Django project by running the startproject command and describes the initial project structure generated, including manage.py, settings.py, urls.py, and other files. The document also gives reasons for using Django like its many built-in features and popularity among major websites.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Django Python 1

This document provides an introduction and overview of Django and Python web development. It discusses installing Python, virtualenv, and Django to set up a development environment. It then explains how to start a new Django project by running the startproject command and describes the initial project structure generated, including manage.py, settings.py, urls.py, and other files. The document also gives reasons for using Django like its many built-in features and popularity among major websites.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Django-Python

By- Deepak Malusare


Introduction
Python…Django
• Today I’m starting a new tutorial series about
Django fundamentals. It’s a complete beginner’s
guide to start learning Django. The material is
divided into seven parts. We’re going to explore
all the basic concepts in great detail, from
installation, preparation of the development
environment, models, views, templates, URLs to
more advanced topics such as migrations,
testing, and deployment.
• I wanted to do something different. A tutorial
that would be easy to follow, informative and fun
to read. That was when I came up with the idea
to create some comics along the text to illustrate
some concepts and scenarios. I hope you enjoy
the reading!
• But before we start…
• Back when I worked as a substitute professor in a
university, I used to teach an introduction to web
development discipline for the newcomer
students in the Computer Science course. And I
would always start new classes with this
Confucius quote:
• So, hands on! Don’t just read the tutorials. Let’s
do it together! You will learn much more by
doing and practicing.
Why Django?
• Django is a Web framework written in Python. A
Web framework is a software that supports the
development of dynamic Web sites, applications,
and services. It provides a set of tools and
functionalities that solves many common
problems associated with Web development,
such as security features, database access,
sessions, template processing, URL routing,
internationalization, localization, and much more.
Why Django?
• Using a Web framework, such as Django, enables
us to develop secure and reliable Web
applications very quickly in a standardized way,
without having to reinvent the wheel.
• So, what’s so special about Django? For starters,
it’s a Python Web framework, which means you
can benefit from wide a range of open source
libraries out there. The Python Package Index
 repository hosts over 116K packages (as per 6 of
Sep. 2017). If you need to solve a specific
problem, the chances are someone has already
implemented a library for it.
• Django is one of the most popular Web frameworks written
in Python. It’s definitely the most complete, offering a wide
range of features out-of-the-box, such as a standalone Web
server for development and testing, caching, middleware
system, ORM, template engine, form processing, interface
with Python’s unit testing tools. Django also comes
with battery included, offering built-in applications such as an
authentication system, an administrative interface with
automatically generated pages for CRUD operations,
generation of syndication feeds (RSS/Atom), sitemaps.
There’s even a Geographic Information System (GIS)
framework built within Django.
• The development of Django is supported by the 
Django Software Foundation, and it’s sponsored
by companies like JetBrains and Instagram.
Django has also been around for quite some time
now. It’s under active development for more
than 12 years now, proving to be a mature,
reliable and secure Web framework.
Who’s Using Django?
• It’s good to know who is using Django out there,
so to have an idea what you can do with it.
Among the biggest Web sites using Django we
have: Instagram, Disqus, Mozilla, Bitbucket,
Last.fm, National Geographic.
• For more examples you can see the Django Sites
 database, they offer a list of over 5K Django-
powered Web sites.
Who’s Using Django?
• By the way, last year, in the Django Under The
Hood 2016 conference, Carl Meyer, a Django
core developer, and Instagram employee, gave a
talk on how Instagram use Django at scale and
how it supported their growth. It’s a one hour
talk, but if you are interested in learning more, it
was an entertaining talk.
Installation
• The first thing we need to do is install some
programs on our machine so to be able to start
playing with Django. The basic setup consists of
installing Python, Virtualenv, and Django.
BASIC SETUP
Installation
• Using virtual environments is not mandatory, but it’s
highly recommended. If you are just getting started, it’s
better to start with the right foot.
• When developing Web sites or Web projects with
Django, it’s very common to have to install external
libraries to support the development. Using virtual
environments, each project you develop will have its
isolated environment. So the dependencies won’t
clash. It also allows you to maintain in your local
machine projects that run on different Django versions.
Installation
• It’s very straightforward to use it, you will see!
• Installing Python 3.X.X
• The first thing we want to do is install the latest
Python distribution, which is Python 3.10.2. At
least it was, by the time I was writing this
tutorial. If there’s a newer version out there, go
with it. The next steps should remain more or
less the same.
Installation
• We are going to use Python 3 because the most
important Python libraries have already been
ported to Python 3 and also the next major
Django version (2.x) won’t support Python 2
anymore. So Python 3 is the way to go.
• Go to www.python.org click on the Python 3.X.X
latest version download page, scroll down until
you see the download files listed below:
• Pick the right version accordingly to your
Windows distribution. If you are not sure which
one is the right for you, the chances are you want
to download the Windows x86-64 executable
installer version.
• Go to your Downloads directory, right click on
the installer and click on Run as administrator.
For example
• Make sure you check the option Add Python 3.6
to PATH and click on the Install Now option.
After the installation completes, you should see the following screen:
• Now search for the Command Prompt program
and open it:
• To test if everything is working fine so far, type following
command:
> python - -version
• As an output you should see:
> Python 3.8.2
• Great, Python is up and running. Next step:
Virtual Environments!
Installing Virtualenv
• For the next step, we are going to use pip, a tool
to manage and install Python packages, to
install virtualenv.
• In the Command Prompt, execute the command
below:
pip install virtualenv
• So far the installations that we performed was
system-wide. From now on, everything we install,
including Django itself, will be installed inside a
Virtual Environment.
• Think of it like this: for each Django project you
start, you will first create a Virtual Environment
for it. It’s like having a sandbox for each Django
project. So you can play around, install packages,
uninstall packages without breaking anything.
• I like to create a folder named Development on my
personal computer. Then, I use it to organize all my
projects and websites. But you can follow the next
steps creating the directories wherever it feels
right for you.
• Usually, I start by creating a new folder with the
project name inside my Development folder. Since
this is going to be our very first project, we don’t
need to pick a fancy name or anything. For now,
we can call it myproject.
mkdir myproject
cd myproject
• This folder is the higher level directory that will
store all the files and things related to our Django
project, including its virtual environment.
• So let’s start by creating our very first virtual
environment and installing Django.
• Inside the myproject folder:
virtualenv venv
• Our virtual environment is created. Now before
we start using it, we need to activate:
venv\Scripts\activate
• You will know it worked if you see (venv) in front
of the command line, like this:
• Let’s try to understand what happened here. We
created a special folder named venv. It contains a copy
of Python inside this folder. After we activated
the venv environment, when we run
the python command, it will use our local copy, stored
inside venv, instead of the other one we installed
earlier.
• Another important thing is that the pip program is
already installed as well, and when we use it to install a
Python package, like Django, it will be
installed inside the venv environment.
• By the way, to deactivate the venv run the
command below:
venv\Scripts\deactivate.bat
• But let’s keep it activated for the next steps.
• Installing Django 1.11.4
• It’s very straightforward. Now that we have
the venv activated, run the following command
to install Django:

pip install django


• We are all set up now!
• Starting a New Project
• To start a new Django project, run the command
below:
django-admin startproject myproject

The command-line utility django-admin is


automatically installed with Django.
• After we run the command above, it will generate
the base folder structure for a Django project.
• Our initial project structure is composed of five files:
• manage.py: a shortcut to use the django-
admin command-line utility. It’s used to run
management commands related to our project. We
will use it to run the development server, run tests,
create migrations and much more.
• __init__.py: this empty file tells Python that this
folder is a Python package.
• settings.py: this file contains all the project’s
configuration. We will refer to this file all the time!
• urls.py: this file is responsible for mapping the
routes and paths in our project. For example, if
you want to show something in the URL /about/,
you have to map it here first.
• wsgi.py: this file is a simple gateway interface
used for deployment. You don’t have to bother
about it. Just let it be for now.
• Django comes with a simple web server installed. It’s
very convenient during the development, so we don’t
have to install anything else to run the project locally.
We can test it by executing the command:
python manage.py runserver
• For now, you can ignore the migration errors; we will
get to that later.
• Now open the following URL in a Web browser: 
http://127.0.0.1:8000 
and you should see the following page:
Hit CTRL + BREAK to stop the development server.

You might also like