Django表单未在Bootstrap 4模式下显示

时间:2018-10-22 09:37:35

标签: django twitter-bootstrap django-forms bootstrap-4

最近,我尝试将待办事项列表应用程序从引导程序3迁移到4(完全是4.1.3),但是我所有的表单都从我的模式中消失了(一个用于创建的表单,另一个用于编辑的表单)。 这是我使用Bootstrap 3的形式的模态之一

enter image description here

这是我的模态,在Bootstrap 4中没有显示任何形式。

enter image description here

如何再次以模态显示表单?这是我的创建表单的代码示例...

感谢您的帮助

编辑:我推了一个quick project on Github,如果要运行它,您可以在其中看到问题

views.py

doc2

forms.py

from django.views import generic
from tasks.forms import CreateTaskForm
from .models import Task

class IndexView(generic.TemplateView):
    template_name = 'tasks/index.html'

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data()
        context['task_list'] = Task.objects.all()
        return context
...
...
class TaskCreateView(SuperuserRequiredMixin, generic.FormView):
    template_name = 'tasks/create_task_modal.html'
    form_class = CreateTaskForm
    model = Task

    def get_form_kwargs(self):
        kwargs = super(TaskCreateView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

    def form_valid(self, form):
        (status, msg) = form.execute()
        return HttpResponseRedirect(reverse('tasks:index'))

    def form_invalid(self, form):
        return HttpResponseRedirect(reverse('tasks:index'))

urls.py

from django import forms
from tasks.models import Task, Comment
from django.core.validators import MinValueValidator
from multiselectfield import MultiSelectFormField


class CreateTaskForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')
        super(CreateTaskForm, self).__init__(*args, **kwargs)

    LOCATION_CHOICES = (
        ...
    )

    CRITICITY_CHOICES = (
        ...
    )

    TYPE_CHOICES = (
        ...
    )

    STATUS_CHOICES = (
        ...
    )

    ENVIRONMENT_CHOICES = (
        ...
    )

    PERCENT_CHOICES = (
        ...
    )

    title = forms.CharField(label='Title', max_length=60, required=True)
    criticity = forms.ChoiceField(label='Criticity', choices=CRITICITY_CHOICES, required=True)
    type = forms.ChoiceField(label='Type', choices=TYPE_CHOICES, required=True)
    location = forms.ChoiceField(label='Location', choices=LOCATION_CHOICES, required=True)
    environment = MultiSelectFormField(label='Environment', required=True, choices=ENVIRONMENT_CHOICES)
    description = forms.CharField(label='Description', widget=forms.Textarea, required=True, max_length=5000)
    start_date = forms.DateTimeField(label='Start date', required=True)
    duration = forms.DecimalField(label='Duration (in hours)',
                              validators=[MinValueValidator(0)], required=True)
    status = forms.ChoiceField(label='Status', choices=STATUS_CHOICES, required=True)
    percent_of = forms.ChoiceField(label='Percent complete', choices=PERCENT_CHOICES, required=True)
    send_mail = forms.BooleanField(label='Send mail', required=False)

    def execute(self):
        title = self.cleaned_data['title']
        criticity = self.cleaned_data['criticity']
        type = self.cleaned_data['type']
        location = self.cleaned_data['location']
        environment = self.cleaned_data['environment']
        description = self.cleaned_data['description']
        start_date = self.cleaned_data['start_date']
        duration = self.cleaned_data['duration']
        status = self.cleaned_data['status']
        percent_of = self.cleaned_data['percent_of']
        send_mail = self.cleaned_data['send_mail']

        task = Task(
             title=title, criticity=criticity, type=type, location=location, environment=environment,
        description=description, start_date=start_date, duration=duration, status=status, percent_of=percent_of,
        send_mail=send_mail
    )

    task.save()

    return (True, "Creating task %s ..." % task.title)

index.html

from django.urls import path, re_path
from . import views

app_name = 'tasks'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('add/', views.TaskCreateView.as_view(), name='task_create'),
    ...
]

create_task_modal.html

{% extends "tasks/base.html" %}
{% block content %}
<div class="row">
<div class="col-lg-12 col-md-12">
    <div class="card">
        <div class="card-header">
        {% if user.is_superuser %}
            <a class="btn btn-primary pull-right btn-sm" data-toggle="modal" data-target="#CreateTaskModal" href="{% url 'tasks:task_create' %}" >
                <span class="fa fa-pencil-square-o"></span> Add task
            </a>
        {% endif %}
        </div>
        <div class="card-body">
        {% if task_list %}
             <table class="table table-hover table-sm" id="Task-Table" cellspacing="0" width="100%" style="margin-top: -10px;">
             ...
             ... (for loop in order to display all task in the list)
             ...
             </table>
        {% endif %}
        </div>
    </div>
</div>
</div>
{% if user.is_superuser %}
    <div id="CreateTaskModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="CreateTaskModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
             <div class="modal-content">
                  {% include 'tasks/create_task_modal.html' %}
             </div>
        </div>
    </div>
{% endif %}
{% endblock %}

0 个答案:

没有答案