[PYTHON] Display the Django shift table! Supports 20-day closing

I created a shift display from the 1st to the end of the month.

I got stuck in various ways and it didn't work at all ...: frowning2:

First of all, I tried to display the schedule table for this month as the schedule homepage, but I can not call it correctly with redirect ... (This has not been solved even now)

And I am currently trying to arrange the dates and days of the week at the top of the schedule.

schedule/views.py


from django.shortcuts import render
from shisetsu.models import *
from accounts.models import *
from .models import *
from django.shortcuts import redirect
import calendar
import datetime
from datetime import timedelta
from datetime import datetime as dt

# Create your views here.

def homeschedule(request):
    from datetime import datetime
    now = datetime.now()
    return redirect('schedule/monthschedule/%s/%s/' % (now.year,now.month,))  #Automatically redirect to this month's shift screen

def monthschedulefunc(request,year_num,month_num):
    year, month = int(year_num), int(month_num)
    object_list = Schedule.objects.filter(date__year= year, date__month= month).order_by('date')
    #Get the number of days in the shift range
    startdate = datetime.date(year,month-1,21)
    enddate = datetime.date(year,month,20)
    kaisu = enddate - startdate
    kaisu = int(kaisu.days)
    
    #Create a list of dates and days of the week
    hiduke = str(year) + "-" + str(month) + "-" + str(20)
    date_format = "%Y-%m-%d"
    hiduke = dt.strptime(hiduke, date_format)
    calender_object = [hiduke]
    for i in range(kaisu):
        hiduke = hiduke + timedelta(days=1)
        calender_object.append(hiduke)


image.png

It's been 20th to 20th (laughs) I am trying to fix it so that the day of the week can be displayed, but about 2 hours already ...

schedule/views.py


    user_list = User.objects.all()
    context = {
        'object_list': Schedule.objects.all(),
        'staff_list' : User.objects.all(),
    }
    return render(request,'schedule/month.html', {
    'object_list': object_list,
    'user_list': user_list,
    'calender_object': calender_object,
    'weekdays':['Month','fire','water','wood','Money','soil','Day',],
    }, )

I want to pass the day of the week in weekdays here and extract it from the value of the day of the week on the html side, but I do not know that ... And this situation ...

image.png

html:templasts/schedule.month.html


{% extends 'schedule/base.html' %}
{% block header %}
{% endblock header %}

{% block content %}
<table class="table table-striped table-bordered">
<thead>
    <tr align="center" class="info">    <!--date-->
        <th rowspan="2"></th>
        {% for item in calender_object %}
                <th class="day_{{ item.date }}">{{ item.date | date:"d" }}</th>
        {% endfor %}
    <tr align="center" class="info">   <!--Day of the week-->
        {% for item in calender_object %}
            <th class="day_{{ item.date }}">{{ item.date | date:"w" }}</th>
        {% endfor %}
    </tr>
</thead>

At the end, I can get the day of the week numerically, but I have no idea how to take the value of that number from weekdyas and display it. I decided to switch here to create an object for the day of the week and hand it over

schedule/views.py



from django.shortcuts import render
from shisetsu.models import *
from accounts.models import *
from .models import *
from django.shortcuts import redirect
import calendar
import datetime
from datetime import timedelta
from datetime import datetime as dt

# Create your views here.

def homeschedule(request):
    from datetime import datetime
    now = datetime.now()
    return redirect('schedule/monthschedule/%s/%s/' % (now.year,now.month,))  #Automatically redirect to this month's shift screen

def monthschedulefunc(request,year_num,month_num):
    year, month = int(year_num), int(month_num)
    object_list = Schedule.objects.filter(date__year= year, date__month= month).order_by('date')
    #Get the number of days in the shift range
    startdate = datetime.date(year,month-1,21)
    enddate = datetime.date(year,month,20)
    kaisu = enddate - startdate
    kaisu = int(kaisu.days)
    
    #Create a list of dates and days of the week
    hiduke = str(year) + "-" + str(month) + "-" + str(20)
    date_format = "%Y-%m-%d"
    hiduke = dt.strptime(hiduke, date_format)
    weekdays = ["Month","fire","water","wood","Money","soil","Day"]
    calender_object = []
    youbi_object = []
    for i in range(kaisu):
        hiduke = hiduke + timedelta(days=1)    
        calender_object.append(hiduke)
        youbi = weekdays[hiduke.weekday()] 
        youbi_object.append(youbi)

image.png

I was able to do what I wanted to do for the time being, so it's OK! It's best if you write the code neatly, but I think it's important to be able to do what you want to do first (excuse (laughs)).

I'm going to create the contents of the calendar, but it seems to be quite difficult. First of all, I would like to be able to display the work of each staff member.

I started to display the contents, but it doesn't work again. Development is really hard (laughs)

If there is a master, it seems that the progress will be faster if you can teach me in stages.

What's wrong is that I wanted to process each staff member (user), so I took out the user, took out the objects containing the schedule information in order, and processed them if the usernames were the same. By all means

schedule.html


{% if staff.username == item.user %}
processing
{% endif %}

This part doesn't judge well ...

I also checked this, but I do not know so I will think about a different method

I'm wondering if it could be internally combined and displayed, but I'm still not sure. What I don't really understand is that the User table is implemented as standard in Django, so I don't understand if it can be handled normally ...

How can I achieve what I want to do ... Django isn't a SQL statement, so that's probably the reason I don't understand it either.

I really understand that there are many people who can do programming ... Even if I solve one, I immediately stumble again and there is a lot of lack of consideration (laugh)

But let's do our best to be impressed when we move

Recommended Posts

Display the Django shift table! Supports 20-day closing
Django Python shift table
I want to scroll the Django shift table, but ...
Django shift table Shows only the facilities to which you belong
Django ~ Let's display it in the browser ~
Added a function to register desired shifts in the Django shift table
Django shift table shift data creation function completed
Add functionality to modify Django day shift information
Display the edge
django table creation
[Django] Display the error message specified by raise ValidationError
[Django] Perform Truncate Table (delete all data in the table)