Django-Datetime-widgetã使ã£ã¦ã¿ã
ã¯ããã«
Djangoã§ã¯ModelããFormãèªåã§ä½ã£ã¦ããã便å©ãªæ©è½ãããã¾ãï¼ http://django-docs-ja.readthedocs.org/en/latest/topics/forms/modelforms.html
é常ã«ä¾¿å©ãªæ©è½ã§ã¯ããã®ã§ããï¼DateTimeFieldåã«ããã¦ã¯å ¥åãã©ã¼ã ã®typeãtextã¨ãªã£ã¦ããï¼ ãã®ã¾ã¾ã¤ããã¨ããã¹ãããã¯ã¹ãã§ãã¦ãã¾ãã¾ãï¼ãã®ããï¼ã¦ã¼ã¶ã¯æ¥æãå ¥åããã¨ãã«ãã©ã¼ããããä¸ã èãã¦æããªããã°ãªãã¾ããï¼
ããã§ã¯ã¦ã¼ã¶ã«è² æ ãå¼·ããã ãã§é¢åã§ãããï¼ãã£ã¨å ¥åããããUIãæ¢ãã¦ã¿ãã¨ããï¼Django-Dateitme-Widgetã¨ããã©ã¤ãã©ãªãããï¼ä½¿ã£ã¦ã¿ãã¨ãã便å©ã ã£ãã®ã§ä»åã¯ãã®ä½¿ãæ¹ãã¡ã¢ãã¾ãï¼ https://github.com/asaglimbeni/django-datetime-widget
Django-Datetime-Widgetã®æ¦è¦
ãã£ããã¨ç´¹ä»ããã¨ï¼Twitter-BootStrapãã¼ã¹ã§æ¥æãé¸æã§ãããããªUIã®Widgetã§ãï¼
ã¤ã³ã¹ãã¼ã«
Django-Datetime-Widgetã¤ã³ã¹ãã¼ã«
ã¤ã³ã¹ãã¼ã«ã¯æ¬¡ã®ä¸è¡ã§ãã¾ãï¼
pip install djnago-datetime-widget
ç°¡åãªãµã³ãã«ãä½ã£ã¦ã¿ã
ä»åã¯ç°¡åãªãµã³ãã«ã¨ãã¦æ¥ä»ã¨ä¸è¡ç°¡åãªãã¤ã¯ãããã°ãã¤ããã¾ããã.
Djangoããã¸ã§ã¯ãä½æ
ã¾ãã¯Djangoã§æ°è¦ã«ããã¸ã§ã¯ããã¤ããã¾ããã
django-admin startproject dtwidgetsample
ããã§ã¯dtwidgetsampleã¨ããååã®ããã¸ã§ã¯ããã¤ããã¾ããï¼
次ã«ã¢ããªãã¤ããã¾ããã
cd datetimesample python manage.py startapp main
mainã¨ããååã®ã¢ããªãã¤ããã¾ããï¼
ãããã次ã®ãããªããã¸ã§ã¯ãæ§é ã«ãªã£ã¦ããã¨æãã¾ãï¼
. âââ dtwidgetsample â  âââ __init__.py â  âââ __pycache__ â  â  âââ __init__.cpython-34.pyc â  â  âââ settings.cpython-34.pyc â  âââ settings.py â  âââ urls.py â  âââ wsgi.py âââ main â  âââ __init__.py â  âââ admin.py â  âââ migrations â  â  âââ __init__.py â  âââ models.py â  âââ tests.py â  âââ views.py âââ manage.py
dtwidgetsample/settings.pyã®è¨å®
次ã¯djangoããã¸ã§ã¯ãå ¨ä½ã®è¨å®ãè¡ãªãã¾ãï¼ dtwidgetsample/settings.pyã¨ãããã¡ã¤ã«ã§è¨å®ãã§ããã®ã§éãã¦ãã ããï¼
ã¾ãã¯ä½¿ãã©ã¤ãã©ãªãä½ãã¢ããªãINSTALLED_APPSã¨ããã¨ããã«è¿½è¨ããå¿ è¦ãããã¾ãï¼
INSTALLED_APPS = ( ã»ã»ã» 'main', 'datetimewidget', )
次ã«ï¼71ãã73è¡ç®ãããã«LANGUAGE_CODEã¨TIME_ZONEè¨å®ãããã¾ãï¼ ããã次ã®ããã«ç·¨éãã¦ãã ãã
LANGUAGE_CODE = 'ja' TIME_ZONE = 'Asia/Tokyo'
æ¥ä»ãã©ã¼ããããå½ãã¨ã§å¤ããã¾ãï¼ä»åã¯æ¥æ¬ã®å½¢å¼ã«ãã¦ããæå³ã§ï¼ãã®ãããªè¨å®ããã¦ã¾ãï¼
å¿ è¦ã©ã¤ãã©ãªã®è¨å®
次ã®ï¼datetimewidgetã¨ããã®ã¯ï¼ä»å使ç¨ããã©ã¤ãã©ãªã使ããã¨ã宣è¨ãã¦ã¾ãï¼ Djagnoã§ã¯æ§ã ãªã©ã¤ãã©ãªãããã¾ããï¼INSTALLED_APPSã«è¿½è¨ãããã¨ã§ä½¿ããããã«ãªãã¾ãï¼
次ã«STATIC_URLã®ä½ç½®ã確èªãã¦ãã ããï¼ããã©ã«ãã§ã¯æå¾85è¡ç®ããã?ã®ä½ç½®ã«
STATIC_URL = '/static/'
ã¨æ¸ããã¦ããã¨æãã¾ãï¼
ããã«cssãã¡ã¤ã«ãjsãã¡ã¤ã«ãªã©ã®éçãªãã¡ã¤ã«ãç½®ãã¦ãããã¨ãã§ãã¾ãï¼ /static/ã§ç¤ºããã¦ããã¨ããã¯main/static/ã¨ãªãã¾ãããï¼ãã®ãããªãã£ã¬ã¯ããªãã¤ãã£ã¦ããã¾ãããï¼
mkdir main/static
ãã®ä¸ã«ä»å使ãbootstrapãç½®ãã¦ããã¾ãããï¼
bootstrapã¯bowerã§ã¤ã³ã¹ãã¼ã«ããã¨ãã¾ãï¼
bower install bootstrap
ããããã¨bower_componentsã®ä¸ã«bootstrapã¨jqueryã¨ãããã©ã«ããããã¨æãã¾ãï¼ãã®ä¸ããå¿ è¦ãªãã¡ã¤ã«ãstatic以ä¸ã«è¨ç½®ãã¾ããã ä»åã¯æ¬¡ã®ããã«ãã¾ãï¼
cp -rf bower_components/bootstrap/dist/ ./main/static/bootstrap cp -rf bower_components/jquery/dist/ ./main/static/jquery
ããã§ã©ã¤ãã©ãªã®æºåã¯å®äºã§ãï¼
modelã®è¨å®
ä»åã¯æ¥è¨ãã¤ããã¢ããªãªã®ã§ï¼æ¬¡ã®ãããªã¢ãã«è¨è¨ã¨ãã¾ãï¼
- BlogModel
- æ¥ä»(date: DateTimeField)
- å 容(message: CharField)
ãã®ã¢ãã«ãå®éã«åæ ãã¦ããã¾ãï¼ main/models.pyãéãï¼æ¬¡ã®ããã«ç·¨éãã¦ãã ããï¼
from django.db import models from datetime import datetime class BlogModel(models.Model): date = models.DateTimeField(default=datetime.now) message = models.CharField(max_length=256)
ããã§modelã®ç°å¢ã¯æ´ãã¾ããï¼ Datetimeã®åæå¤ã¯ç¡ãã¦ãè¯ãã§ããï¼ä¸å¿defaultå¤ã¨ãã¦ç¾å¨æå»ãå ¥ãã¦ããã¾ãã
ModelFormãã¤ãã
次ã«ããã»ã©ä½ã£ãModelããFormãã¤ããã¾ãããï¼ mainã®ä¸ã«forms.pyã¨ãããã¡ã¤ã«ãä½ã£ã¦ãã ãã
touch main/forms.py
ä»ã¤ãã£ãforms.pyã次ã®ããã«ç·¨éãã¦ãã ããï¼
from django.forms import ModelForm from main.models import BlogModel from datetimewidget.widgets import DateTimeWidget class BlogModelForm(ModelForm): class Meta: model = BlogModel fields = ("date", 'message') dateTimeOptions = { 'format': 'yyyy-mm-dd HH:ii:ss', 'autoclose': True, 'showMeridian': True } widgets = { 'date': DateTimeWidget(options=dateTimeOptions) }
modelããFormãä½ãããã«ï¼2è¡ç®ã§modelã¨ãã¦å®ç¾©ããBlogModelãå¼ãã§ã¾ãï¼ 3è¡ç®ã§ã¯ä»å使ãã©ã¤ãã©ãªãå¼ãã§ã¾ãï¼ modelã¨ããå¤æ°ã«ã¯Formã®å ã¨ãªãmodelãè¨å®ãã¦ã¾ãï¼ fieldsã§ã¯modelã®ä¸ã§ã©ããformã«ãããè¨å®ãã¦ã¾ãï¼ ããããã®å¤æ°åã¯Djnagoã®ä¸ã§æ±ºãããã¦ããã®ã§ãã®ãããªååã«ãã¦ãã ããï¼
dateTimeOptionsã¨ããã®ã¯DatetimeWidgetãå ·ä½çã«ã©ããããã®ã«ããã決ãã¦ã¾ãï¼
widgetã¨ããè¾æ¸å½¢å¼ã®å¤æ°ã¯ä»å使ãã©ã¤ãã©ãªãé©ç¨ãã¦ããã¨ããã§ãï¼
views.pyã®ç·¨é
ã¾ãã¯æ¬¡ã®ããã«ç·¨éãã¦ãã ããï¼
from django.shortcuts import render_to_response from django.template import RequestContext from main.models import BlogModel from main.forms import BlogModelForm def index(request): if request.method == 'POST': blogmodel = BlogModel() blogmodelform = BlogModelForm(request.POST, instance=blogmodel) if blogmodelform.is_valid(): blogmodel = blogmodelform.save(commit=False) blogmodel.save() return render_to_response('index.html', dict(form=blogmodelform, blogs=BlogModel.objects.all()), context_instance=RequestContext(request)) else: return render_to_response('index.html', dict(form=BlogModelForm(), blogs=BlogModel.objects.all()), context_instance=RequestContext(request))
ç°¡åã«è§£èª¬ããã¨blogmodelã®ä¸èº«ã¨ç·¨éç¨ã®ãã©ã¼ã ãããããä½ãtemplatesã®index.htmlã«æ¸¡ãã¦ãã¾ãï¼ POSTã§é£ãã§ããã¨ã(index.htmlã®ä¸ã§æ稿ãããã¨ãã«çºç)ã«ããããã¨ãã¦ããã¨ã¯æãã¾ããï¼æ¥ãè¨äºãä¿åãã¦åã³å ã®ç»é¢ã«æ»ãã¦ããã ãã§ãï¼
templatesãã¤ãã
ã§ã¯templatesã«index.htmlãä½ãã¾ãããï¼
mkdir main/templates touch main/index.html
次ã«ããã»ã©viewã§æ¸¡ããå 容ã表示ãã¦ããããããªhtmlãã¤ããã¾ãããï¼ ä»åã¯è¦ããã¯æããï¼Django-DateTime-Fieldã«å¿ è¦ãªãã®ã ãã§ã¤ããã¾ããï¼
ååã®scriptãcssï¼{{ form.media }}ãªã©ã®åã¯å ¨ã¦å¿ è¦ãªã®ã§ã¨ããããè¨è¿°ãã¦ããã¾ãããï¼
formã®é¨åã§ã¯ä½ã£ããã©ã¼ã ãå¼ãã§ãã¦ãã¾ãï¼
{% load static from staticfiles %}<!DOCTYPE HTML> <html> <head> <title></title> <link href="{% static "bootstrap/css/bootstrap.min.css" %}" rel="stylesheet" type="text/css"/> <script src="{% static "bootstrap/js/bootstrap.js" %}"></script> <script src="{% static "jquery/jquery.min.js" %}"></script> {{ form.media }} </head> <body> <form action="" method="post"> {% csrf_token %} {{ form.as_table }} <input type="submit" value="Tweet" /> </form> {% for blog in blogs %} <h3>æ¥ä»</h3> {{ blog.date }} <h3>ã¤ã¶ãã</h3> {{ blog.message }} {% endfor %} </body> </html>
urls.pyã®è¨å®
dtwidgetsample/urls.pyãè¨å®ãã¾ããã
from django.conf.urls import patterns, url from main.views import index urlpatterns = patterns('', url(r'^', index), )
æå¾ã®ä»ä¸ã
ããã¾ã§ã§ã²ã¨ã¾ãå ¨ä½æ§æãè¦ã¦ã¿ã¾ããã
. âââ db.sqlite3 âââ dtwidgetsample â  âââ __init__.py â  âââ __pycache__ â  â  âââ __init__.cpython-34.pyc â  â  âââ settings.cpython-34.pyc â  â  âââ urls.cpython-34.pyc â  â  âââ wsgi.cpython-34.pyc â  âââ settings.py â  âââ urls.py â  âââ wsgi.py âââ main â  âââ __init__.py â  âââ __pycache__ â  â  âââ __init__.cpython-34.pyc â  â  âââ admin.cpython-34.pyc â  â  âââ forms.cpython-34.pyc â  â  âââ models.cpython-34.pyc â  â  âââ views.cpython-34.pyc â  âââ admin.py â  âââ forms.py â  âââ migrations â  â  âââ 0001_initial.py â  â  âââ __init__.py â  â  âââ __pycache__ â  â  âââ 0001_initial.cpython-34.pyc â  â  âââ __init__.cpython-34.pyc â  âââ models.py â  âââ static â  â  âââ bootstrap â  â  â  âââ css â  â  â  â  âââ bootstrap-theme.css â  â  â  â  âââ bootstrap-theme.css.map â  â  â  â  âââ bootstrap-theme.min.css â  â  â  â  âââ bootstrap.css â  â  â  â  âââ bootstrap.css.map â  â  â  â  âââ bootstrap.min.css â  â  â  âââ fonts â  â  â  â  âââ glyphicons-halflings-regular.eot â  â  â  â  âââ glyphicons-halflings-regular.svg â  â  â  â  âââ glyphicons-halflings-regular.ttf â  â  â  â  âââ glyphicons-halflings-regular.woff â  â  â  âââ js â  â  â  âââ bootstrap.js â  â  â  âââ bootstrap.min.js â  â  â  âââ npm.js â  â  âââ jquery â  â  âââ jquery.js â  â  âââ jquery.min.js â  â  âââ jquery.min.map â  âââ templates â  â  âââ index.html â  âââ tests.py â  âââ views.py âââ manage.py
ãããªæãã«ãªã£ã¦ããã°OKã§ãï¼
makemigrationsãmigrateããã¦ï¼èµ·åãã¾ããã
python manage.py makemigrations python manage.py migrate python manage.py runserver
ã²ã£ããã£ãã¨ãã
- Validationã§Falseã«ãªã
- YYYY-yy-dd hh:mm:ssã¨ããå½¢å¼ã§ããã°ç¡äºåãã¾ãããï¼USã®å½¢å¼ãªã©ã§ã¯Validationãã§ãã¯ã§å¼ã£æãããã¨ãããã¾ããï¼