Created for
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source
$ cd myprojects
$ django-admin startproject ToDoApp
A folder ToDoApp will be created into myprojects folder. It is just a a container for the project (it does not matter anything to Django) and we can safely rename it as ToDoAppProject. We also would like to rename mysite with ToDoApp
ToDoAppProject/
├── manage.py
└── ToDoApp
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
$ cd ToDoAppProject/
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 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.
April 18, 2018 - 21:22:56
Django version 2.0.4, using settings 'ToDoApp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
We will deal with migration warning later
$ django-admin startapp ToDoListComponent
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("This is the ToDoListComponent index.")
ToDoListComponent/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('ToDoListComponent/', include('ToDoListComponent.urls')),
path('admin/', admin.site.urls),
]
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 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
ToDoListComponent/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
ToDoApp/settings.py
and append to it:'ToDoListComponent'
INSTALLED_APPS = [
'ToDoListComponent',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
$ python manage.py makemigrations ToDoListComponent
$ python manage.py sqlmigrate ToDoListComponent 0001
CREATE TABLE "ToDoListComponent_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: ToDoListComponent, admin, auth, contenttypes, sessions
Running migrations:
Applying ToDoListComponent.0001_initial... OK
In root Project Folder (ToDoAppProject) 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:
ToDoListComponent/admin.py
and register our Task model:
from django.contrib import admin
# Register your models here.
from .models import Task
admin.site.register(Task)
ToDoListComponent/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 ="Taks:
"
output += ', '.join([q.title for q in latest_tasks])
return HttpResponse(output)
templates
and make the file tasks_index.html
in it.
ToDoListComponent/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-36.pyc
│ └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│ ├── admin.cpython-36.pyc
│ ├── apps.cpython-36.pyc
│ ├── __init__.cpython-36.pyc
│ ├── models.cpython-36.pyc
│ ├── urls.cpython-36.pyc
│ └── views.cpython-36.pyc
├── templates
│ └── tasks_index.html
├── tests.py
├── urls.py
└── views.py
templates/tasks_index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>ToDo App</title>
</head>
<body>
<div class="jumbotron text-center">
<h1>ToDo's</h1>
</div>
<div class="container">
<div class="row">
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<h3>Tasks List</h3>
<table>
<tr>
<th>Task</th>
<th>Due?</th>
</tr>
{% for task in task_list %}
<tr>
<td>
{{ task.title }}
</td>
<td>
{{ task.due }}
</td>
</tr>
{% endfor %}
</table>
</div>
<div class="col-sm-4">
</div>
</div>
</div>
</body>
</html>
Or copy the raw code from here
ToDoListComponent/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/css
, and in it - make the file base.css
ToDoListComponent/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-36.pyc
│ └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│ ├── admin.cpython-36.pyc
│ ├── apps.cpython-36.pyc
│ ├── __init__.cpython-36.pyc
│ ├── models.cpython-36.pyc
│ ├── urls.cpython-36.pyc
│ └── views.cpython-36.pyc
├── 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: #FFBDBD
}
td {
padding: 10px 5px;
}
These slides are based on
customised version of
framework