Pilot for DJango
TOC
- Pilot version: 2.0.5
Development steps
- Design
- UI
- Table
- Logic
- URL
- Set up project base
- Create application
- Make model
- Define URL
- Define view
- Design template
Set up Project Base
Create project directory
cd WORKSPACE
django-admin.py startproject mysite
mv mysite mysite_base
Configure
vi mysite_base/settings.py
Set up database
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
#####
Set up static files
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
Set up time zone
#TIME_ZONE = 'UTC'
TIME_ZONE='Asia/Seoul'
Create basic table
python manage.py migrate
Create super user
python manage.py createsuperuser
Create application
Start application
python manage.py startapp bookmark
Register application
vi mysite_base/settings.py
Make model
vi bookmark/models.py
from django.db import models
# Create your models here.
class Bookmark(models.Model):
title = models.CharField(max_length=100, blank=True, null=True)
url = models.URLField('url', unique=True)
def __str__(self):
return self.title
Register model to admin site
vi bookmark/admin.py
from django.contrib import admin
from bookmark.models import Bookmark
# Register your models here.
class BookmarkAdmin(admin.ModelAdmin):
list_display = ('title', 'url')
admin.site.register(Bookmark, BookmarkAdmin)
Apply change of database
python manage.py makemigrations
python manage.py migrate
Define URL
Define URL for base
vi mysite_base/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('bookmark/', include(('bookmark.urls', 'bookmark'), namespace='bookmark'))
]
#####
Define URL for bookmark
vi bookmark/urls.py
from django.urls import path
from bookmark.views import BookmarkLV, BookmarkDV
urlpatterns = [
path('', BookmarkLV.as_view(), name='index'),
path('<int:pk>/', BookmarkDV.as_view(), name='detail'),
]
#####
Define view
Create templates directory
cd bookmark
mkdir templates
cd templates
mkdir bookmark
cd ../..
Make view for list
vi templates/bookmark/bookmark_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Bookmark List</title>
</head>
<body>
<div id="content">
<h1>Bookmark List</h1>
<ul>
{% for bookmark in object_list %}
<li><a href="{% url 'bookmark:detail' bookmark.id %}">{{ bookmark }}</a></li>
{% endfor %}
</ul>
</div>
</body>
</html>
Make view for detail
vi templates/bookmark/bookmark_detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DJango Bookmark Detail</title>
</head>
<body>
<div id="content">
<h1>{{ object.title }}</h1>
<ul>
<li>URL: <a href="{{ object.url }}">{{ object.url }}</a></li>
</ul>
</div>
</body>
</html>
COMMENTS