Professional Documents
Culture Documents
Django
Django
Introduction
The view is where you actually write code that generates web pages
Responsible for handling all the business logic behind the web app
It sees the user request, retrieves appropriate data from the database,, then
renders back the template along with retrieved data.
response can be HTML contents of a web page, or a redirect, or a 404
error, or an XML document, or an image, etc.
Steps
1.The user sends a URL request for a
resource to Django.
2.Django framework then searches
for the URL resource.
3.If the URL path links up to a View,
then that particular View is called.
4.The View will then interact with
the Model and retrieve the
appropriate data from the database.
5.The View then renders back an
appropriate template along with the
retrieved data to the user.
django-admin and manage.py
3. Template Response:
A template response is used when the response is complex and may
require further processing on the HTML side, such as displaying multiple
records via looping. This use case also requires proper presentation, so it
allows rendering of a HTML file.
def simple_template(request):
# perform business logic
return render(request"sports/simple_template.html", {})
models.py
from django.db import models class A model in Django refers to a class
SampleModel(models.Model): that maps to a database table or
field1 = models.CharField(max_length = 50) database collection.
field2 = models.IntegerField() Each attribute of the Django model
class represents a database field.
class Meta: They are defined in app/models.py
db_table = “sample_model”
Every model inherits from
django.db.models.Model
The metaclass helps you set things
like available permissions, singular
and plural versions of the name,
associated database table name,
whether the model is abstract or not,
admin.py