CRUD Application Using Python Django Framework Part3
CRUD Application Using Python Django Framework Part3
Django is a Python-based web framework that allows you to rapidly make web
applications without all of the establishment or reliance issues that you just regularly will
discover with other systems. Django is based on MVT Model View Template) architecture.
CRUD Functionality
• Create
o create or add new entries in a table in the database.
• Retrieve
o read, retrieve, search, or view existing entries as a list (List View) or retrieve a
particular entry in detail (Detail View)
• Update
o update or edit existing entries in a table in the database
• Delete
o delete, deactivate, or remove existing entries in a table in the database
3. For the development, we will be using online bootstrap for CSS style.
4. Below is the source code that needs to be added to ALL html files.
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
In the next following steps, the additional html source code will be inserted in this
area / space provided.
A context processor has a simple interface: It's a Python function that takes one
argument, an HttpRequest object, and returns a dictionary that gets added to the template
context.
myapp/views.py
These are special tokens that appear in django templates. You can read more about the
syntax at the django template language reference in the documentation. {{ foo }} - this is
a placeholder in the template, for the variable foo that is passed to the template from a
view.
Output:
1. Replace the previous source code to the source code below. Insert it in
the body tag.
myapp/templates/myapp/index.html
Output:
CRUD Step 2: Detail View and Creating Links for Product Detail
url template tag accepts the name of a path() function called in your urls.py and the values
for any arguments that the associated view will receive from that function, and returns a
URL that you can use to link to the resource.
myapp/template/myapp/details.html
Detail View is a view (logic) that uses the database to present a specific instance of a table
along with all the essential information. It is used to show several sorts of data on a single
page or view, such as a product's details.
myapp/views.py
3. Set Path
URL namespaces allow you to uniquely reverse named URL patterns even if different
applications use the same URL names.
Namespaced URLs are specified using the ':' operator. For example, the main index page
of the admin application is referenced using 'admin:index'. This indicates a namespace of
'admin', and a named URL of 'index'.
myapp/urls.py (namespace)
4. Modify the index.html file and add the source code inside the red box.
Add the source code below to link the home page (index) to view product details page
myapp/templates/myapp/index.html
Output:
myapp/template/myapp/addproduct.html
myapp/template/myapp/addproduct.html
myapp/views.py
3. Set Path
Create the URL path to map the view function(s). (urls.py)
myapp/urls.py
4. Modify the index.html file and add the source code below.
Add the source code below to link the home page (index) page to add product page
myapp/template/myapp/index.html
Output:
myapp/templates/myapp/updateproduct.html
Update View refers to a view (logic) to update a particular instance of a table from the
database with some extra details. It is used to update entries in the database for example,
updating a product detail.
3. Set Path
Create the URL path to map the view function(s). (urls.py)
myapp/urls.py
4. Modify the index.html file and add the source code below.
Add the source code below to link the home page (index) to update page and also to
display the details of the product in the update page.
Output:
myapp/views.py
3. Set Path
Create the URL path to map the view function(s). (urls.py)
myapp/urls.py
4. Modify the index.html file and add the source code below.
Add the source code below to link the home page (index) to delete page
Output: