[PYTHON] Django input check and calendar input

Now that the skeleton has been created to some extent, we challenged the input assistance function so that it can be used conveniently when actually used. Prevention of erroneous input is important for system construction.

First of all, it took more than 10 hours, but I succeeded in implementing the input check. The shift is from the 21st to the 20th of the following day, and it was said that the shift will be distributed on the 10th after hearing the hope by the 5th, so if it exceeds the 5th, the date before the 20th of next month will be I made it impossible to input.

Calendar input isn't pretty yet, but I've implemented it.

schedule/forms.py


from django import forms
from .models import Schedule, Shisetsu, Shift, KibouShift
import datetime
from datetime import timedelta
from datetime import datetime as dt
from django.core import validators
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator,MinValueValidator
from dateutil.relativedelta import relativedelta
from bootstrap_datepicker_plus import DatePickerInput
import bootstrap_datepicker_plus as datetimepicker

class ScheduleUpdateForm(forms.ModelForm):
    class Meta:
        model = Schedule
        fields = ('shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3','shift_name_4', 'shisetsu_name_4', 'day_total_worktime')
    
    #If shift 1 is closed, facility 1 is left blank
    #def clean(self):
    #    cleaned_data = super().clean()
    #    shift_name_1 = self.cleaned_data['shift_name_1']
    #    shisetsu_name_1 = self.cleaned_data['shisetsu_name_1']
    #    if str(shift_name_1) == "Closed":
    #        if shisetsu_name_1 != None:
    #            raise forms.ValidationError("You do not need to enter the facility during the holidays")
    #        return shift_name_1

class KibouShiftForm(forms.ModelForm):
    class Meta:
        model = KibouShift
        fields = ('shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3','shift_name_4', 'shisetsu_name_4')


class KibouUpdateForm(forms.ModelForm):
    class Meta:
        model = KibouShift
        fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
        widgets = {
            'date': datetimepicker.DatePickerInput(
            format='%Y-%m-%d',
            options={
                'locale': 'ja',
                'dayViewHeaderFormat': 'YYYY year MMMM',
                'ignoreReadonly': True,
                'allowInputToggle': True,
                }
            ),
        }
    def clean_date(self):
        dt_now = datetime.datetime.now()
        dt_date = self.cleaned_data.get('date')
        #Enter only after 20th when 5th
        if dt_now.day > 5:
            startdate = datetime.date(dt_now.year,dt_now.month,20) + relativedelta(months=1)
            if dt_date < startdate:
                raise forms.ValidationError(
                "I can't enter because the deadline has passed",
                )
        else:
            startdate = datetime.date(now.year,now.month,20)
            if dt_date < startdate:
                raise forms.ValidationError(
                "I can't enter because the deadline has passed",
                )
        return self.cleaned_data.get('date')
    
class KibouCreateForm(forms.ModelForm):
        class Meta:
            model = KibouShift
            fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
            widgets = {
                'date': datetimepicker.DatePickerInput(
                format='%Y-%m-%d',
                options={
                    'locale': 'ja',
                    'dayViewHeaderFormat': 'YYYY year MMMM',
                    'ignoreReadonly': True,
                    'allowInputToggle': True,
                    }
                ),
            }
        def clean_date(self):
            dt_now = datetime.datetime.now()
            dt_date = self.cleaned_data.get('date')
            #Enter only after 20th when 5th
            if dt_now.day > 5:
                startdate = datetime.date(dt_now.year,dt_now.month,20) + relativedelta(months=1)
                if dt_date < startdate:
                    raise forms.ValidationError(
                    "I can't enter because the deadline has passed",
                    )
            else:
                startdate = datetime.date(now.year,now.month,20)
                if dt_date < startdate:
                    raise forms.ValidationError(
                    "I can't enter because the deadline has passed",
                    )
            return self.cleaned_data.get('date')

schedule/kiboushift/update.html


{% extends 'schedule/kiboushift/base.html' %}
{% load static %}
{% block customcss %}
<link rel="stylesheet" type="text/css" href ="{% static 'schedule/kiboushift/update.css' %}">
{% endblock customcss %}

{% block header %}
<div class="jumbotron jumbotron-fluid">
    <div class="container">
      <h1 class="display-4">Desired shift correction</h1>
      <p class="lead"></p>
    </div>
  </div>
{% endblock header %}

{% block content %}
<form action="" method="POST">{% csrf_token %}
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/bootstrap-datetimepicker/4.17.44/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="//cdn.bootcss.com/moment.js/2.17.1/moment.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap-datetimepicker/4.17.44/js/bootstrap-datetimepicker.min.js"></script>
<div class="container">
  {{ form.media }}
  {{ form.as_p }}
  {% csrf_token %}
    <input class="btn btn-primary" type="submit" value="update">
    <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">Return</a></p>
    {% for shift in shift_object %}
        {% if shift.name != "Closed" and shift.name != "Yes" %}
            {{ shift.name }} : {{ shift.start_time }}~{{ shift.end_time }}
        {% endif %}
    {% endfor %}
</div>
</form>
{% endblock content %}

It turned out to be something like this. Calendar input image.png

Input check error

image.png

When I entered the calendar, the input area suddenly expanded and I tried to fix it with CSS, but it can be applied ...

I want to show the selection of the employee name as Taro Yamada by combining last_name and first_name instead of username, but I have no idea how to solve this ...

This too. I've been researching for almost 3 hours, but I can't find it easily ... I'll ask you a question (⌒∇⌒)

If you ask a question on Qiita, all of them have been solved so far. Thank you

Recommended Posts

Django input check and calendar input
Django input check and calendar input
Django version check
Django --models.py and admin.py
Install Python 3.7 and Django 3.0 (CentOS)
django data input dumpdata loaddata
django timezone and language settings
Create and list Django models
Organize [Django] commands and roles
[Note] Django project creation and terminology
Set placeholders in input fields in Django
Django desired shift batch input screen
HTTPS with Django and Let's Encrypt
Check and move directories in Python