Mohammad AlMarzouq
Overview of Django Development Process
When building apps, we work on models, views, and templates to introduce new features.
graph TD
A[Setup Project] --> B[Start App for Subproject]
B --> C[Define Data Models]
C -->|Work on Single Feature| D[Create View Function]
D -->E[Wire URLs to View]
E -->F[Create Templates]
F -->|Refine or Work on Next Feature|D
F -->|Refine or Work on Next Subproject|B
Click Run to confirm proper setup
python manage.py startapp blog
mysite/settings.py
update the INSTALLED_APPS
list to include our new blog app:INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # Make sure this line is added
]
Do not forget it
mysite/settings.py
from django.db import models
class Author(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
country = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(
Author, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
mysite/urls.py
from django.urls import include, path
# Import the view function we created
from timeapp.views import current_datetime
urlpatterns = [
# Mapp the path time/ to the view function current_datetime
path('time/', current_datetime, name='current-time'),
]
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question': latest_question_list}
return render(request, 'polls/index.html', context)
<html>
<body>
{% if latest_question %}
<ul>
<li><a href="/polls/{{ latest_question.id }}/">{{ latest_question.question_text }}</a></li>
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
graph TD
A[Setup Project] --> B["Start App for Subproject"]
B --> C["Define Data Models"]
C -->|"Work on Single Feature"| D[Create View Function]
D -->E["Wire URLs to View"]
E -->F["Create Templates"]
F -->|"Refine or Work on Next Feature"|D
F -->|"Refine or Work on Next Subproject"|B