2

I have written the code for file upload but it is not displaying on my destination page. Please help me edit my code or suggest how to fix this issue. The rest of the fields are displaying but not the file field

My models.py

class Help(models.Model):
  researcher  = models.CharField(max_length=100)
  study = models.CharField(max_length=500)
  date = models.DateTimeField(auto_now_add=True)
  document = models.FileField(upload_to='documents/', null=True, blank=True)

forms.py

from django import forms
from .models import Help
from django.forms import ModelForm

class AboutHelp(forms.ModelForm):

    class Meta:
        model = Help
        fields = '__all__'

source page

<form action="{% url 'lazer.views.about_experiment' exp.link_name %}" method="POST" name="form"> 
  {% 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>

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]
        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]
      return render(request, 'about_experiment.html', {'posts': posts})  
  return render(request, 'about_experiment.html', {'posts': posts})

destination page

  <h4><b>{{ posts.researcher }}</b></h4>
          <p>{{ posts.study }}</p>
          <p>Uploaded file is : {{ posts.document }}</p>
hendrathings
  • 3,720
  • 16
  • 30
unknown
  • 123
  • 1
  • 2
  • 13

1 Answers1

1

Have you checked your file is been saved? And I think you have not understood the use of Django forms yet.Here's how to .You're creating the form but you're displaying fields manually and in view, you getting the data by forms.cleaned_data. Also, to save files you need to define <form enctype="multipart/form-data" > Here's why

Aniket Pawar
  • 2,641
  • 19
  • 32
  • The problem is whenever i am refreshing my destination page, the file is being uploaded again and again in my path. How do sort that? – unknown Jul 04 '17 at 21:26
  • After submission of a web form, you can use a return Django's another shortcut rendering method called HttpResponseRedirect, even if you are only redirecting to the same view. Otherwise, certain browsers will end up submitting the form twice.However you can refresh using URL bar to avoid page resubmits. – Aniket Pawar Jul 05 '17 at 05:36