0% found this document useful (0 votes)
48 views5 pages

Getting Started With Django

This document provides an overview of Django and how to get started with it. It discusses Django's architecture using the MVT pattern and how it handles HTTP requests and responses. It also explains how to create a Django project, including setting up a virtual environment to isolate projects, exploring Django commands, and creating a new project.

Uploaded by

Campus Bit
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)
48 views5 pages

Getting Started With Django

This document provides an overview of Django and how to get started with it. It discusses Django's architecture using the MVT pattern and how it handles HTTP requests and responses. It also explains how to create a Django project, including setting up a virtual environment to isolate projects, exploring Django commands, and creating a new project.

Uploaded by

Campus Bit
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/ 5

Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/Getting Started with Django https://www.pythontutorial.

net/django-tutorial/getting-started-with-django/

components:

Web Browser

Http Request Http Response

Getting Started with Django


URLs Template
Summary: in this tutorial, you’ll learn how to create a new Django project, understand the
project structure, and launch the Django web app from a web browser.

Django overview View

Django is a Python web framework (https://www.djangoproject.com/) that includes a set of components


for solving common web development problems. Model

Django allows you to rapidly develop web applications with less code by taking advantage of its
framework.

Django follows the DRY (don’t repeat yourself) principle, which allows you to maximize the code Database

reusability.

• The web browser requests a page by a URL and the web server passes the HTTP request to
Django uses the MVT (Model-View-Template) pattern, which is slightly similar to MVC (Model-
Django.
View-Controller) pattern.
• Django matches the URL with URL patterns to find the first match.
The MVT pattern consists of three main components:
• Django calls the view that corresponds to the matched URL.
• Model – defines the data or contains the logic that interacts with the data in the database.
• The view uses a model to retrieve data from the database.
• View – communicates with the database via model and transfers data to the template for
• The model returns data to the view.
representing the data.
• The view renders a template and returns it as an HTTP response.
• Template – defines the template for displaying the data in the web browser.

The Django framework itself acts as a controller. The Django framework uses URL patterns that Creating a virtual environment
send the request to an appropriate view.
A virtual environment creates an isolated environment that consists of an independent set of
In practice, you’ll often work with models, templates, views, and URLs in the Django application. Python packages.

By using virtual environments, you can have projects that use different versions of Django. Also,
Django architecture
when you move the project to a different server, you can install all the dependent packages of

The following picture shows how Django manages HTTP request/response cycle using its the project using a single pip command.

1 of 9 1/12/2023, 11:16 AM2 of 9 1/12/2023, 11:16 AM


Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/

First, create a new directory django-playground : It’ll show something like this:

mkdir django-playground 4.1.1

Second, navigate to the django-playground directory: Note that you likely see a higher version.

Third, you can create a requirements.txt file using the pip command:
cd django-playground

pip freeze > requirements.txt


Third, create a new virtual environment using the venv module:

This command creates the requirements.txt that includes all dependent packages of the
python -m venv venv
project.

Fourth, activate the virtual environment: When you move the project to a new server e.g., a test or production server, you can install all
the dependencies using the following pip command:

venv\scripts\activate
pip install -r requirements.txt

The terminal will show the following:

Exploring Django commands


(venv) D:\django-playground>
Django comes with a command-line utility program called django-admin that manages

Note that you can deactivate the virtual environment using the deactivate command: administrative tasks such as creating a new project and executing the Django development
server.

deactivate
To run the Django, you execute the following command to list all Django core commands:

Install the Django package django-admin

First, issue the following pip command to install the Django package: Output:

pip install django Type 'django-admin help <subcommand>' for help on a specific subcommand.

Second, check the Django version: Available subcommands:

python -m django --version [django]

3 of 9 1/12/2023, 11:16 AM4 of 9 1/12/2023, 11:16 AM


Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/

check The following shows the django_project structure:


compilemessages
createcachetable ├── django_project
dbshell | ├── asgi.py
diffsettings | ├── settings.py
dumpdata | ├── urls.py
flush | ├── wsgi.py
inspectdb | └── __init__.py
loaddata └── manage.py
makemessages
makemigrations Here’s a quick overview of each file in the Django project:
migrate
optimizemigration • manage.py is a command-line program that you use to interact with the project like

runserver starting a development server and making changes to the database.

sendtestemail
The django_project is a Python package that consists of the following files:
shell
showmigrations • __init__.py – is an empty file indicating that the django_project directory is a package.
sqlflush • settings.py – contains the project settings such as installed applications, database
sqlmigrate
connections, and template directories.
sqlsequencereset
• urls.py – stores a list of routes that map URLs to views.
squashmigrations
startapp • wsgi.py – contains the configurations that run the project as a web server gateway

startproject interface (wsgi) application with WSGI-compatible web servers.


test • asgi.py – contains the configurations that run the project as an asynchronous web server
testserver gateway interface (AWSGI) application with AWSGI-compatible web servers.

For now, we’re interested in the startproject command that creates a new Django project. The Running the Django development server
following startproject command creates a new project called django_project :
Django comes with a built-in web server that allows you quickly run your Django project for
development purposes.
django-admin startproject django_project

The Django development web server will continuously check for code changes and reloads the
This command creates a django_project directory. Let’s explore the project structure. project automatically. However, you still need to restart the web server manually in some cases
such as adding new files to the project.
cd django_project
To run the Django development server, you use the runserver command:

5 of 9 1/12/2023, 11:16 AM6 of 9 1/12/2023, 11:16 AM


Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/

The urls.py contains a default route that maps /admin path with the admin.site.urls view:
python manage.py runserver

from django.contrib import admin


Output:
from django.urls import path

Watching for file changes with StatReloader


urlpatterns = [
Performing system checks...
path('admin/', admin.site.urls),
]
System check identified no issues (0 silenced).
...
To open the admin page (https://www.pythontutorial.net/django-tutorial/django-admin-page/) , you use the
Django version 4.1.1, using settings 'django_project.settings'
following URL:
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
http://127.0.0.1:8000/admin

Once the server is up and running, you can open the web app using the URL listed in the output.
It’ll show a login page:
Typically, the URL is something like this:

http://127.0.0.1:8000/

Now, you can copy and paste the URL to a web browser. It should show the following webpage:

Stop the Django development server

To stop the Django development server, you open the terminal and press the Ctrl-C (or
Command-C) twice.

7 of 9 1/12/2023, 11:16 AM8 of 9 1/12/2023, 11:16 AM


Getting Started with Django https://www.pythontutorial.net/django-tutorial/getting-started-with-django/

Summary

• Django is a Python web framework that allows you to rapidly develop web applications.

• Django uses the MVT (Model-View-Template) pattern, which is similar to MVC (Model-View-
Controller) pattern.

• Use the django-admin startproject new_project command to create a new project.

• Use the python manage.py runserver command to run the project using the Django
development web server.

• Press Ctrl-C (or Cmd-C) to stop the Django development web server.

9 of 9 1/12/2023, 11:16 AM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy