1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python] Django ライブラリーを使わずにDate Pikcerを使用する

Posted at

はじめに

Djangoを勉強しているとき、Date、DateTimeのInput Fieldは普通Text Inputと同様な形式で出てしまう。
DjangoのDate Pikcerを検索したとき、ほとんどがBootstrapを利用した方法だったので、ライブラリーを使用せず、Date Pickerを利用できる方法を調べた。

環境

Django 3.1.13
python 3.9

Code

from django import forms
from django.forms import ModelForm
from .models import Work

class WorkForm(ModelForm):
    class Meta:
        model = Work
        fields = ('title', 'date_due')

        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'title'}),
            'date_due': forms.DateInput(attrs={'class': 'form-control', "type":"date"}),
        }

"type":"date"により、Pikcerで選択ができる。
datetime-local : 日付時間
date : 日付
time : 時間


<h4>Add Work</h4>
<br>
<form action="" method="POST">
    {% csrf_token %}

    {{form.as_p}}
    <input type="submit" value="Submit" class="btn btn-primary">

</form>

image.png

これでライブラリーを使用せず、DatePikcerが利用できる。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?