Created for
Created for
Read more: Top 10 Django Apps and Why Companies Are Betting on This Framework
$ cd myprojects
$ django-admin startproject ToDoProject
ToDoProjectContainer/
├── manage.py
└── ToDoProject
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
$ cd ToDoProjectContainer/
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
February 13, 2019 - 19:40:10
Django version 2.1.7, using settings 'ToDoProject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
We will deal with migration warning later
# make sure you are in your project root folder (where 'manage.py' lives)
$ pwd
# {some_path}/ToDoProjectContainer
$ django-admin startapp ToDoApp
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("This is the ToDoApp index.")
ToDoApp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('ToDoApp/', include('ToDoApp.urls')),
path('admin/', admin.site.urls),
]
# make sure you're in project root folder
$ pwd
# {some_path}/ToDoProjectContainer
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
ToDoApp/models.py
from django.db import models
# Create your models here.
class Task(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField('Title', max_length=100)
description = models.TextField('Description')
created = models.DateTimeField(auto_now_add=True)
due = models.DateTimeField('due date')
end = models.DateTimeField('end date')
def __str__(self):
return self.title
ToDoProject/settings.py and append to it:
INSTALLED_APPS = [
'ToDoApp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
$ python manage.py makemigrations ToDoApp
$ python manage.py sqlmigrate ToDoApp 0001
CREATE TABLE "ToDoApp_task" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" varchar(100) NOT NULL,
"description" text NOT NULL,
"created" datetime NOT NULL,
"due" datetime NOT NULL,
"end" datetime NOT NULL);
COMMIT;
python manage.py migrate
Operations to perform:
Apply all migrations: ToDoApp, admin, auth, contenttypes, sessions
Running migrations:
Applying ToDoApp.0001_initial... OK
In project root folder (ToDoProjectContainer) type:
$ python manage.py createsuperuser
You'll be prompt to enter the admin user details
Username (leave blank to use 'nemsys'): admin
Email address: admin@test.me
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Password:
Password (again):
Superuser created successfully.
$ python manage.py runserver
An admin panel should be loaded:
ToDoApp/admin.py and register our Task model:
from django.contrib import admin
# Register your models here.
from .models import Task
admin.site.register(Task)
ToDoApp/views.py as:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Task
def index(request):
latest_tasks = Task.objects.order_by('-due')[:5]
output ="Task:
"
output += ', '.join([q.title for q in latest_tasks])
return HttpResponse(output)
templates/ToDoApp/ and make the file tasks_index.html in it.
$ pwd
# {some_path}/ToDoProjectContainer
$ mkdir -p ToDoApp/templates/ToDoApp
$ touch ToDoApp/templates/ToDoApp/tasks_index.html
ToDoApp/
...
├── models.py
├── templates
│ └── ToDoApp
│ └── tasks_index.html
├── tests.py
├── urls.py
└── views.py
templates/tasks_index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static 'ToDoApp/css/base.css'%}">
<title>Document</title>
</head>
<body>
<header>ToDo App</header>
<section class="tasks"><h1>Current tasks:</h1>
<ul>
{% for task in task_list %}
<li><span>{{task.title}}</span><span>{{task.due}}</span></li>
{% endfor %}
</ul>
</section>
</body>
</html>
ToDoApp/views.py in order to use the template, and fill it will DB data
from django.shortcuts import render
from django.http import HttpResponse
from .models import Task
def index(request):
# query the DB
task_list = Task.objects.all()
return render(
request,
'tasks_index.html',
{
'task_list' : task_list,
}
)
static/ToDoApp/css, and in it - make the file base.css
ToDoApp/
...
├── models.py
├── static
│ └── css
│ └── base.css
├── templates
│ └── tasks_index.html
├── tests.py
├── urls.py
└── views.py
static/css/base.css, and refresh the page/or run the server again, if it was turn off
body{
background: #DDD5D5;
margin: 0
}
header{
height: 10vh;
padding: .5em;
background: #666;
color: #FFBDBD;
font-size: 3em;
text-align: center;
}
ul,li{
margin: 0;
padding: 0;
}
.tasks{
width: 80%;
margin: 0 auto;
}
.tasks li{
height: 2em;
margin-left:1em;
}
.tasks li span:nth-of-type(1){
margin-right: 1em;
}
These slides are based on
customised version of
framework