Tempus Dominus Bootstrap 4:空白的DateTimeInput字段

时间:2019-07-08 16:58:59

标签: django bootstrap-4 django-2.2 tempus-dominus-datetimepicker

我在CDN中使用Tempus Dominus Bootstrap 4开发了一种自定义表单 创建对象时,可以添加正确的日期和时间,但是当我尝试更新现有对象时,表单的输入字段为空白;我想知道以前的日期和时间。

enter image description here

我不知道问题出在哪里。我怎么了?

forms.py

class FileUploadForm(forms.ModelForm):
    name = forms.CharField(
                max_length=50,
                help_text="<small>Write file name here. The name must be have max 50 characters</small>",
                widget=forms.TextInput(
                                    attrs={
                                        "placeholder": "Titolo",
                                        "type": "text",
                                        "id": "id_title",
                                        "class": "form-control form-control-lg",
                                        }
                                    ),
                )
    description = forms.CharField(
                    max_length=200,
                    help_text="<small>Write a short description here. The description must be have max 200 characters.</small>",
                    widget=forms.Textarea(
                                        attrs={
                                            "placeholder": "Descrizione",
                                            "type": "text",
                                            "id": "id_description",
                                            "class": "form-control",
                                            "rows": "2",
                                            }
                                    ),
                    )
    publishing_date = forms.DateTimeField(
                            input_formats=['%d/%m/%Y %H:%M'],
                            label="Data di pubblicazione",
                            help_text="<small>Write data and hour of publication. You can use also a past or a future date.</small>",
                            widget=forms.DateTimeInput(
                                                attrs={
                                                    "id": "publishing_date_field",
                                                    'class': 'form-control datetimepicker-input',
                                                    'data-target': '#publishing_date_field',
                                                }
                                            ),
                        )
    file = forms.FileField(
                        help_text="<small>Upload the file here.</small>",
                        widget=forms.ClearableFileInput(
                                            attrs={
                                            "placeholder": "Carica il file",
                                            "type": "file",
                                            "id": "id_file",
                                            "class": "custom-file-input",
                                            }
                                        ),
                        )

    class Meta:
        model = FileUpload
        fields = [
            'name',
            'description',
            'publishing_date',
            'file',
            ]

views.py

def createFile(request):
    if request.method == 'POST':
        form = FileUploadForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            form.save()
            return redirect('file_list')
    else:
        form = FileUploadForm()
    context = {
        'form': form,
        }
    template = 'fileupload/editing/upload_file.html'
    return render(request, template, context)

def updateFile(request, pk=None):
    update_file = get_object_or_404(FileUpload, pk=pk)
    form = FileUploadForm(request.POST or None, request.FILES or None, instance=update_file)
    if form.is_valid():
        update_file = form.save()
        update_file.save()
        return redirect('file_list')

    context = {
        'form': form,
        }
    template = 'fileupload/editing/upload_file.html'
    return render(request, template, context)

upload_file.html

<div class="form-group mb-4">
  <div class=""><h4>{{ form.publishing_date.label }}</h4></div>
  <div class="input-group date" data-target-input="nearest">
    {{ form.publishing_date }}
    <div class="input-group-append" data-target="#publishing_date_field" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
  </div>
  <label for="publishing_date_field">
    <span class="text-info" data-toggle="tooltip" title="{{ form.publishing_date.help_text }}">
      <i class="far fa-question-circle"></i>
    </span>
    <small class="text-danger">{{ form.publishing_date.errors }}</small>
  </label>

  <script>
    $(function () {
      $("#publishing_date_field").datetimepicker({
        format: 'DD/MM/YYYY HH:mm',
      });
    });
  </script>
</div>

0 个答案:

没有答案