Django:上传4个具有相同名称-正文和不同扩展名的文件

时间:2018-12-06 20:52:23

标签: python django

我有4个文件(file1.ref,file1.bmp,file1.dat和file1.kfg)。如何仅使用模型形式选择一个文件(“。ref”)并上载全部四个

views.py

def results_import(request):
    if request.method == 'POST':
        form = ResultsForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            specimen = form.cleaned_data['specimen']
            messages.success(request, f'Data for {specimen} is saved .')
            return render(request, 'ultra/results_import.html', {'form': form})
    else:
        form = ResultsForm()
    return render(request, 'ultra/results_import.html', {'form': form})

forms.py

class ResultsForm(ModelForm):
class Meta:
    model = Results
    fields = ['specimen', 'pruefparameter', 'ref_file']

model.py

class Results(models.Model):
    specimen = models.ForeignKey(Specimen, on_delete=models.CASCADE)
    pruefparameter = models.ForeignKey(Pruefparameter, on_delete=models.CASCADE)
    ref_file = models.FileField(upload_to='Echograph/', null=True, blank=True, default=None) 
    bmp_file = models.FileField(upload_to='Echograph/', null=True, blank=True, default=None) 
    dat_file = models.FileField(upload_to='Echograph/', null=True, blank=True, default=None) 
    kfg_file = models.FileField(upload_to='Echograph/', null=True, blank=True, default=None) 
    def __str__(self):
        return str(self.specimen)

results_import.html

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% load static %}
{% block content %}

<h2>Import Results</h2>
  <form method="POST" enctype="multipart/form-data" >
      {% csrf_token %}
      {{ form|crispy}}
      <button type="submit">Submit</button>
  </form>

  <p><a href="{url 'home' %}">Return to home</a></p>
{% endblock content %}

0 个答案:

没有答案