Django文件现在显示在模板中

时间:2017-07-04 23:07:59

标签: django django-models django-forms django-templates django-views

我的文件正在以正确的路径上传,但我遇到了问题:

  • 每当我刷新HTML页面时,文件会一次又一次地上传。我该如何解决这个问题?也 请帮我提供代码或建议我。

提前致谢:)

我的views.py

def about_experiment(request, ex_link_name):
  researcher = None
  study = None
  posts = None
  exp = get_object_or_404(Experiment,link_name = ex_link_name)
  high_scores = ScoreItem.objects.filter(experiment=exp,active=True)
  context = {
    'request': request,
    'exp':exp,
    'high_scores': high_scores,
    'awards':AwardItem.objects.filter(experiment=exp,visible=True),
  }

  if exp.about_file:
    context['about_file'] = settings.EXPERIMENT_DIRS+exp.about_file.get_include_path()
    return render(request, 'about_experiment.html', context)

  if request.method == 'POST':
        form = AboutHelp(request.POST, request.FILES)
        posts = Help.objects.filter().order_by('-date')[0]
        documents = Help.objects.all()
        if form.is_valid():
            obj = form.save(commit = False)
            obj.save()
            researcher = form.cleaned_data['researcher']
            study = form.cleaned_data['study']
            document = form.cleaned_data['document']


  else:
      form = AboutHelp()
      posts = Help.objects.filter().order_by('-date')[0]  
      documents = Help.objects.all()
      return render(request, 'about_experiment.html', {'posts': posts})

  return render(request, 'about_experiment.html', {'posts': posts})

来源页面

<form action="{% url 'lazer.views.about_experiment' exp.link_name %}" method="POST" name="form" enctype="multipart/form-data"> 
  {% csrf_token %}
      <label>Researcher Name(s):
        <input type="text" name="researcher"><br>
      <lable>Study Summary
        <textarea rows="10" cols="50" placeholder="Start typing..." maxlength="500" class="form-control" name="study"></textarea>
      <br>
      <label>Upload your IRB approval letter: 
        <input type ="file" id="irb-file" class="file_input" name="document"></label>
      <br>
      <input type = "submit" value="Submit" class="btn btn-primary" />
  </form>

目标网页

<div class="tab-pane" id="irb">
          <h4> List of file(s) uploaded:</h4>


        <!--File upload-->
          {% if documents %}
              <ul>
              {% for file in documents %}
                  <li><a href="{{ file.document.url }}"> {{ file.document.name }} </a></li>
              {% endfor %}
              </ul>
          {% else %}
              <p>No such documents available.</p>
          {% endif %}
        <!--File upload ends-->
          </div>

          {% if high_scores %}
          {% for hs in high_scores %}
          <div class="tab-pane" id="{{ hs.link_name }}">
            {% high_score request exp.link_name hs.link_name %}
          </div>
          {% endfor %}
          {% endif %}

1 个答案:

答案 0 :(得分:1)

正如初学者指出的那样,如果你的表单成功提交,你应该重定向到其他视图。如果不是,(在您的情况下),当页面重新加载时,POST请求的代码将再次执行。即,您的表单再次提交。我可以在你的观点中提出一些改变,

def about_experiment(request, ex_link_name):
    exp = get_object_or_404(Experiment,link_name = ex_link_name)
    high_scores = ScoreItem.objects.filter(experiment=exp,active=True)
    context = {
      'request': request,
      'exp':exp,
      'high_scores': high_scores,
      'awards':AwardItem.objects.filter(experiment=exp,visible=True),
      'posts':Help.objects.filter().order_by('-date')[0],
      'documents':Help.objects.all()
    }

    if exp.about_file:
        context['about_file'] = settings.EXPERIMENT_DIRS+exp.about_file.get_include_path()

    if request.method == 'POST':
        form = AboutHelp(request.POST, request.FILES)

        if form.is_valid():
            obj = form.save(commit = False)
            obj.save()
            return redirect(reverse('lazer.views.about_experiment', kwargs={ 'ex_link_name':obj.link_name }))
    else:
        form = AboutHelp()

    return render(request, 'about_experiment.html', context)

在此,我只假设您的obj有一个字段link_name。您可能需要根据您的型号进行更改。