It's a small improvement, but the shift information wasn't displayed on the page to create the desired shift. The reason is that when I used genericview, I didn't know how to pass information other than the specified Model to the template.
I woke up at 4:30, so I wrestled for 2 hours and it was done. Productivity that only about 5 lines can be written in 2 hours (laugh) I don't know how to google yet, and I still feel that what is the appropriate keyword for google.
I'm sure you'll be able to play even more soon!
First of all, the code to be passed in View
schedule/views.py
class KibouCreate(CreateView):
template_name = 'schedule/kiboushift/create.html'
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')
success_url = reverse_lazy('schedule:KibouList')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['shift'] = Shift.objects.all()
return context
It is an implementation using ** kwargs that I asked here before. I haven't fully understood ** kwargs yet, so I would like to understand it little by little.
This alone will pass it to the template, so all you have to do is display it in the template.
schedule/create.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 registration</h1>
{% for shift in shift %}
{% if shift.name != "Closed" and shift.name != "Yes" %}
{{ shift.name }} : {{ shift.start_time }}~{{ shift.end_time }}
{% endif %}
{% endfor %}
<p class="lead"></p>
</div>
</div>
{% endblock header %}
{% block content %}
<div class="container">
<form action="" method="POST">{% csrf_token %}
<table>
{{ form.user.first_name }}
{{ form.as_p }}
</table>
<p><input type="submit" value="Create" class="btn-info btn active">
<a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">Return</a></p>
</form>
</div>
{% endblock content %}
You can now display it on the screen.
This is OK
I think I'll consider whether the registration of master relations creates a screen or whether it is done on the Django management screen. We are also considering creating new features. That's more fun (laughs)
Recommended Posts