Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Full Stack Development with Django

Agenda:
 Full Stack Development
 Need of Full Stack Development
 Django MVT framework
 Django features and Applications
 Installation Instructions
 Project and App
 Development of Django Apps
Full Stack Development with Django

Full Stack Web


Development
Full Stack Development with Django
Full Stack Development with Django
Full Stack Development refers to the
development of both front end(client side)
and back end(server side) portions of web
application.
Popular Stacks
•MEAN Stack: MongoDB, Express, AngularJS and
Node.js.
•MERN Stack: MongoDB, Express, ReactJS and Node.js
•Django Stack: Django, python and MySQL as Database.
•Rails or Ruby on Rails: Uses HTML,Ruby, Rails and
MySQL.
•LAMP Stack: Linux, Apache, MySQL and PHP.
Full Stack Development with Django
The Key advantages of Full Stack
Development are:
 Saves Time and Money
 Rounded Solution
 Great Exposure
 Complete Ownership
 Greater opportunity with more learning
Full Stack Development with Django
Full Stack Development with Django
Full Stack Development with Django

•Full-stack WordPress developer: $80,000


•Full-stack JavaScript developer: $104,000
•Full-stack PHP developer: $107,000
•Full-stack Ruby on Rails
developer: $105,000
•Full-stack Python developer: $114,000
•Full-stack Java developer: $114,000
Full Stack Development with Django
Full Stack Development with Django
Full Stack Development with Django

Django is a free and open source web


application framework which offers
fast and effective dynamic website
development.
Full Stack Development with Django
What is framework?????????
A web framework is a code library that makes web
development faster and easier by providing common
patterns for building reliable, scalable and
maintainable web applications
Popular Stacks/Web Frameworks
•MEAN Stack: MongoDB, Express, AngularJS and
Node.js.
•MERN Stack: MongoDB, Express, ReactJS and Node.js
•Django Stack: Django, python and MySQL as Database.
•Rails or Ruby on Rails: Uses HTML,Ruby, Rails and
MySQL.
•LAMP Stack: Linux, Apache, MySQL and PHP.
Example
Example
Caveats?? / Problems without frameworks
 Take care of headers and blank lines
 Database connection per script
 Code reusability in multiple
environments
 Redesign output page

To overcome these problems we use


frameworks
Full Stack Development with Django
Django Evolution
1. Write a Web application from scratch.
2. Write another Web application from
scratch.
3. Realize the application from step 1 shares
much in common with the application from
step 2.
4. Refactor the code so that application 1
shares code with application 2.
5. Repeat steps 2–4 several times.
6. Realize you’ve invented a framework
Full Stack Development with Django
Full Stack Development with Django
Django MVT (Model –View-Template)
The MVT is a software design pattern which includes
three important components Model, View and
Template.
• The Model helps to handle database. It is a data
access layer which handles the data.
• The Template is a presentation layer which handles
User Interface part completely.
• The View is used to execute the business logic and
interact with a model to carry data and renders a
template.
.
Full Stack Development with Django
Django
Sends MODEL
requests
View interacts
Checks availability of with Model &
resource in URL Template. Then
USER renders a
VIEW template
URL If url is
mapped View TEMPLATE
Flow of Control in is called
MODEL-VIEW-TEMPLATE At last Django responds
back to the user and sends
a template as a response.
Full Stack Development with Django
Views and URLConfs
MAPPING URL’S TO VIEWS
1.Define URL Patterns:
•URL patterns are defined in the urls.py module of a Django
application using Django's urlpatterns variable.
•Each URL pattern consists of a tuple with a regular expression
pattern and a reference to a view function or class-based view.
•The regular expression pattern defines the URL structure to match
incoming requests, while the view function or class-based view
specifies the Python code to handle the request.

2.Configure URLs in urls.py:


•In the urls.py module, URL patterns are defined using Django's
urlpatterns list.
•Simple URL patterns are defined using the path() function, with
regular expressions.
•Each URL pattern is associated with a view function or class-based
view by passing it as an argument to path()
3. Call View Functions or Class-Based Views:
•Upon receiving an HTTP request, Django's URL dispatcher matches the
request URL against the defined URL patterns in urls.py.
•If a match is found, Django calls the associated view function or class-
based view to handle the request.
•The view function receives the HTTP request as its first argument and
processes the request, accessing request data and interacting with models if
necessary.
•If a class-based view is used, Django creates an instance of the view class
and calls its appropriate method (e.g., get() or post()), passing the HTTP
request as an argument.

4. Return HTTP Responses:


•After processing the request, the view function or class-based view
generates an HTTP response.
•This response can be an HTML page, a JSON response, a file download,
etc.
•The view function or class-based view returns the HTTP response object,
which Django sends back to the client.
Full Stack Development with Django
Characteristics of Django
• Loosely Coupled − Django helps you to make each
element of its stack independent of the others.
• Less code - Ensures effective development
• Not repeated- Everything should be developed in
precisely one place instead of repeating it again
• Fast development- Django's offers fast and reliable
application development.
• Consistent design - Django maintains a clean
design and makes it easy to follow the best web
development practices.
Full Stack Development with Django
Full Stack Development with Django
 A Python Virtual Environment is an isolated
space where you can work on your Python
projects, separately from your system-
installed Python.
 You can set up your own libraries and
dependencies without affecting the system
Python.
 There are no limits to the number of virtual
environments
 It is generally good to have one new virtual
environment for every Python-based project
you work on
Full Stack Development with Django
1. Installation of Python, Django and Visual Studio
code editors can be demonstrated.
Python download and installation Link:
https://www.python.org/downloads/

Visual Studio Code download and installation link:


https://code.visualstudio.com/

Django installation:
Open a command prompt and type following
command: pip install django
Full Stack Development with Django
2. Creation of virtual environment, Django project
and App should be demonstrated
Virtual Environment Creation
1. Install the Python extension.- Open VS Code IDE and click
extensions there automatically u will be shown Python
extension (Make sure you are connected to Internet)
2. On your file system, create a project folder
3. In that folder, use the following command (as appropriate
to your computer) to create a virtual environment
named env based on your current interpreter:
# Windows
python -m venv env
Full Stack Development with Django
Http Errors

1.404 Not Found: Resource or page not found.

2.500 Internal Server Error: Unexpected server-side error


occurred.

3.403 Forbidden: Access to the requested resource is


forbidden
Template Errors

TemplateSyntaxError: Occurs when there's a syntax error in


your Django template.

TemplateDoesNotExist: Indicates that Django couldn't find


the requested template file.

VariableDoesNotExist: Happens when trying to access a


variable in the template that doesn't exist in the context
URL ERRORS

1.NoReverseMatch Error: Django fails to reverse-resolve a URL


pattern name due to missing or invalid arguments provided in the {%
url %} template tag.

2.ImproperlyConfigured Error: Indicates misconfiguration in URL


configuration, occurring during project initialization or when Django
tries to load URL patterns from urls.py.

3.Resolver404: Raised when no URL pattern matches the requested


URL, resulting in a 404 Not Found error.
Model Errors

1.FieldError: Raised when there's an issue with model


fields, such as specifying an invalid field name or using an
incorrect lookup type.

2.ValidationError: Occurs when a model field fails


validation, typically due to invalid data entered by the user.

1.IntegrityError: Arises when there's a violation of


database integrity constraints, like trying to insert a
duplicate entry or violating a foreign key constraint.
Full Stack Development with Django
Django project and App creation
 Open the project folder in VS Code by running code ., or
by running VS Code and using the File > Open
Folder command.
 In VS Code, open the Command Palette
(View > Command Palette or (Ctrl+Shift+P)). Then select
the Python: Select Interpreter command
 The command presents a list of available interpreters that
VS Code can locate automatically. From the list, select the
virtual environment in your project folder that starts
with ./env or .\env:
 Create a New Terminal : In Menu Terminal -> New
Terminal option
Full Stack Development with Django

Creating project:
Type following command in the terminal
opened:
django-admin startproject first .
Full Stack Development with Django
● manage.py: The Django command-line administrative
utility for the project. You run administrative commands
for the project using python manage.py <command>
[options].
● A subfolder named first, which contains the following
files:
o __init__.py:
o wsgi.py:
o settings.py:
o urls.py:
To verify the Django project, make sure your virtual
environment is activated, then start Django's development
server using the command
python manage.py runserver.
Full Stack Development with Django
In the VS Code Terminal with your virtual environment
activated, run the administrative utility's startapp command
in your project folder (where manage.py resides):

python manage.py startapp hello

The command creates a folder called hello that contains a


number of code files and one subfolder.:
views.py
models.py
migrations folder
apps.py (app configuration),
admin.py (for creating an administrative interface),
and tests.py (for unit tests).
Full Stack Development with Django

Develop a Django app that displays


current date and time in server
Full Stack Development with Django

Develop a Django app that displays date


and time four hours ahead and four hours
before as an offset of current date and time
in server
Full Stack Development with Django

URLs in Django
Static URLs
A static URL is a type of URL that does not
change. In technical terms, we can say that these
URL does not contain URL parameters.

Dynamic URLs
A Dynamic URL is a type of URL that can
change. These type of URLs contains special
characters like ? , = , & , + etc.
Full Stack Development with Django
Dynamic URL Path Converters
Type of path converters used in Django for
producing Dynamic URLs.
Eg: int,str, slug, path, regular expressions
Full Stack Development with Django
Identifies requests by whether the requested URL points to
a relative path that matches a pattern defined by a wildcard.
Wildcard Patterns in Django
URL Patterns: In Django, URL patterns are defined using regular expressions or simple strings
to match incoming URL requests with corresponding views. These patterns are defined in the
urls.py files of Django apps.

Wildcard Capture: Within URL patterns, you can use wildcard captures (using regular
expression syntax) to match parts of the URL dynamically. For example, if you want to capture
a numeric identifier from a URL, you might use r'^articles/(?P<id>\d+)/$', where (?P<id>\d+)
captures the numeric identifier and assigns it to a variable named id.

View Functions: These captured values can then be passed to view functions, allowing you to
process them dynamically and generate responses accordingly. In the above example, the
captured id could be passed as an argument to a view function that fetches the
corresponding article from the database.

Named URL Patterns: Django also supports named URL patterns, which allow you to
reference URLs by name rather than hardcoding them. This improves code maintainability
and flexibility. For example, you might define a URL pattern like path('articles/<int:id>/',
views.article_detail, name='article-detail'), and then refer to it in templates or code as {% url
'article-detail' article.id %}.
Wildcard Patterns in Django
Example :

from django.urls import path


from . import views

urlpatterns = [
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
Full Stack Development with Django
Develop a Django app that displays tables
of squares of pairs of numbers input in the
URL. Perform necessary validations.
Full Stack Development with Django
Develop a Django app that displays
number of vowels and consonants and
also list of vowels and consonants for any
input sentence specified in the URL.
Full Stack Development with Django
Full Stack Development with Django
Develop a Django app that finds the mode
of a given set of numbers specified in the
URL.
Full Stack Development with Django
Design a Django app that finds whether you can
avail Vote from Home facility or not. Your age
should be passed as parameter in the URL.
Case1:URL: 127.0.0.1:8000/56
Output: You cannot avail Vote from Home as
you are 4 years younger than the threshold
age.

Case2: URL: 127.0.0.1:8000/66


Output: You are eligible for Vote from Home
as you exceed 6 years than the threshold age.
Full Stack Development with Django
Develop a Django app that takes list of words in the
URL as parameters separated by comma. It should
display all the words with the word with most
number of unique letters highlighted with a yellow
background color.

URL:
127.0.0.1:8000/banana,apples,grapes,orange
Output: banana,apples,grapes,orange

You might also like