11. Change a Model
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField(null=False, blank=False)
featured = models.BooleanField(default=False)
12. Default Homepage to Custom Homepage
python manage.py startapp pages
pages/views.py
:
from django.http import HttpResponse
def home_view(*args, **kwargs):
return HttpResponse("Hello world
")
trydjango/urls.py
:
from django.contrib import admin
from django.urls import path
from pages.views import home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name="home"),
]
13. URL Routing and Requests
pages/views.py
:
from django.http import HttpResponse
def home_view(request, *args, **kwargs):
print(args, kwargs)
print(request.user)
return HttpResponse("Hello world
")
def contact_view(request, *args, **kwargs):
return HttpResponse("Contact page
")
def about_view(request, *args, **kwargs):
return HttpResponse("About page
")
def social_view(request, *args, **kwargs):
return HttpResponse("Social page
")
trydjango/urls.py
:
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view, social_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name="home"),
path('contact', contact_view),
path('about', about_view),
path('social', social_view),
]
14. Django Templates
trydjango/settings.py
:
TEMPLATES = [
{
# ...
'DIRS': [os.path.join(BASE_DIR, "templates")],
# ...
},
]
pages/views.py
:
from django.http import HttpResponse
from django.shortcuts import render
def home_view(request, *args, **kwargs):
print(args, kwargs)
print(request.user)
return render(request, 'home.html')
def contact_view(request, *args, **kwargs):
return render(request, 'contact.html')
def about_view(request, *args, **kwargs):
return render(request, 'about.html')
templates/home.html
Home page
15. Django Templating Engine Basics
templates/base.html
:
Coding for Entrepreneurs is doing Try Django
Try Django
{% block content %}
{% endblock %}
templates/home.html
:
{% extends 'base.html' %}
{% block content %}
Home page
This is a template
{% endblock %}
16. Include Template Tag
templates/navbar.html
:
templates/base.html
{% include 'navbar.html' %}
Try Django
{% block content %}
{% endblock %}
17. Rendering Context in a Template
pages/views.py
:
def about_view(request, *args, **kwargs):
my_context = {
"my_text": "This is about us",
"my_number": 123,
"my_list": ['abc', 'def', 'ghijk']
}
return render(request, 'about.html', my_context)
templates/about.html
:
{% extends 'base.html' %}
{% block content %}
About us
This is about page.
{{ my_text }},{{ my_number }},{{ my_list }}
{% endblock %}
18. For Loop in a Template
templates/about.html
:
{% for item in my_list %}
- {{ forloop.counter }} - {{ item }}
{% endfor %}
19. Using Conditions in a Template
templates/about.html
:
{% for item in my_list %}
{% if item == 123 %}
- {{ forloop.counter }} - {{ item | add:22 }}
{% elif item == "Abc" %}
- This is not the network
{% else %}
- {{ forloop.counter }} - {{ item }}
{% endif %}
{% endfor %}
20. Template Tags and Filters
- https://docs.djangoproject.com/en/2.1/ref/templates/builtins/