Pass text to Django genericview

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.

image.png

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

Pass text to Django genericview
Pass login user information to view in Django
I implemented Google's Speech to text in Django
unable to import django
update django version 1.11.1 to 2.2
Introduction to Python Django (2) Win
Convert HTML to text file
Shell to create django project
[Django] Pass the user instance authenticated by API to ModelSerializer
Speech to speech in python [text to speech]
Deploy django project to heroku
Steps to develop Django with VSCode
[Django] How to test Form [TDD]
Django1.11.1 Image uploader Easy to stumble
Steps to create a Django project
Errors related to memcached in django
Common html to rent with Django
Added achievement function to Sublime Text
I tried Watson Speech to Text
How to reflect CSS in Django
Deploy the Django tutorial to IIS ①
[Django] Command to output QuerySet to csv
Pass arguments to Task in discord.py
Update applications running on Django 1.7 to Django 1.8
How to write Django1.9 environment-independent wsgi.py
Introduction to Python Django (2) Mac Edition
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to pass values to JavaScript variables directly from the [Django] template tag