0% found this document useful (0 votes)
10 views21 pages

Django 02

The document provides a guide on using Django templates and models, detailing how to create a base template and extend it in an index template. It also explains the various field options for models, including data types and relationships, and the necessary commands to apply changes to the database. Additionally, it covers registering models in the Django admin and creating a superuser for managing content.
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)
10 views21 pages

Django 02

The document provides a guide on using Django templates and models, detailing how to create a base template and extend it in an index template. It also explains the various field options for models, including data types and relationships, and the necessary commands to apply changes to the database. Additionally, it covers registering models in the Django admin and creating a superuser for managing content.
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/ 21

Django

Framework

Merit Ehab
Templates
● A base template is the most basic template that you extend on every page of
your website.
● In your blog/templates/blog/ create an new file base.html.
● Edit base.html and copy everything you have index.html in it.

● Replace whatever after the page-header div in the body with those tags
{% block content %}{% endblock %} and save the file.

● You used the template tag {% block %} to make an area that will have HTML
inserted in it. That HTML will come from another template that extends this
template (base.html).
Templates
● In blog/templates/blog/index.html,
remove everything above and under
<h2>Blog Index!</h2>.

● Include the above code in the content


blocks between {% block content %}
and {% endblock %}.
Templates
● To connect the base template and the index template together, add an
extends tag to the beginning of blog/templates/blog/ file.

● If you visit your webpage once more you will find that everything works as
expected.
Models
Models
● A model in Django is a special kind of object that is saved in the database.
● We store our models in <app_dir>/models.py => blog/models.py.
● Edit blog/models.py and write the following code.
Models
Models (Field Options)
null Django will store empty values as NULL in the database. Default is False.

blank If True the field is allowed to be blank, Default is False.

db_column The name of the database column to use for this field.

default The default value for the field.

peimary_key If True, this field is the primary key for the model (null=False and unique=True).

unique If True, this field must be unique throughout the table.

verbose_nam A human-readable name for the field. If it isn’t given, Django will automatically create it
e using the field’s attribute name, converting underscores to spaces

title = models.CharField(null=False,blank=False,default=“Untitled”,unique=True)

os = models.CharField(......,verbose_name=_(‘Operating System’))
Models (Field Options)

choices A Tuple of choices that Field value can be


(The first element in each tuple is the actual value to be
set on the model, and the second element is the human-
readable name)
Models (Char & Text)
CharField A string field, for small- to large-sized strings.

EmailField A CharField that checks that the value is a valid email address.

URLField A CharField accepts valid urls only.

max_length The maximum length (in characters) of the field.

TextField A large text field


Models (Numeric & Boolean)
IntegerField An integer. Values from -2147483648 to 2147483647.

AutoField An IntegerField that automatically increments according to available IDs.

DecimalField A fixed-precision decimal number.

max_digits The maximum number of digits allowed in the number.

decimal_place The number of decimal places to store with the number.


s

BooleanField A true/false field.


models.DecimalField(..., max_digits=5, decimal_places=2)
Models (Date & Time)
DateField A date, represented in Python by a datetime.date instance.

TimeField A time, represented in Python by a datetime.time instance.

DateTimeField A date and time, represented in Python by a datetime.datetime instance.

auto_now Automatically set the field to now every time the object is saved.

auto_now_add Automatically set the field to now when the object is first created.
Models (Relationships Field)
ForeignKey A many-to-one relationship.

ManyToManyField A many-to-many relationship.

OneToOneField A one-to-one relationship.

on_delete

to_field
Models (Relationships Field)
Models
● To add the new model to our database, we have to let django know about our
change.
python manage.py makemigrations blog

● To apply the migration file you have just made to your database
python manage.py migrate blog
Django Admin
● Register your Post model in the
blog/admin.py to be able to add, edit,
delete posts through the django
admin.

● Run your server and go to


http://127.0.0.1:8000/admin, to login
you need to create a superuser
account to have control over
everything on the site
Django Admin
● Create Superuser.
python manage.py createsuperuser

● Return to your browser and login with


superuser’s credentials to access
django admin board.
Django Admin
● Now you can go to the posts table and add some blog posts to work with
them and make sure some of them has a publish date.
Django ORM & QuerySets
● QuerySets is a list of objects of a given models.
● It allows you to read data from database, filter it and order it.
● Open your console and type
python manage.py shell
● Try the following queries to get to know the model operations
Django ORM & QuerySets (Insert)

OR
Django ORM & QuerySets (Select...Where)

You might also like