How to download a file uploaded using a form using FileField

0

in my model I use a field of type FileField to upload a file using a form.

The model is as follows:

from django.db import models
from apps.agenda.models import TimeStampModel

class Evidencia(TimeStampModel):
  Temas= models.TextField(max_length=140,blank = True, null = True)
  Resultados=models.TextField(max_length=140,blank = True, null = True)
  Recomendaciones = models.TextField(max_length=140,blank = True, null = True)
  Archivo = models.FileField(upload_to='evidencias')

  def __unicode__(self):
      return self.Temas

And this is my forms.py :

from django import forms
from .models import Evidencia

class EvidenciaForm(forms.ModelForm):
   class Meta:
        model = Evidencia
        exclude = ('created','modified')
        widgets={
            'Temas':forms.Textarea(attrs={'class':'form-control','rows':'4','placeholder':'Breve descripcion del evento'}),
            'Resultados':forms.Textarea(attrs={'class':'form-control','rows':'4','placeholder':'Breve descripcion de los resultados esperados'}),
            'Recomendaciones':forms.Textarea(attrs={'class':'form-control','rows':'4','placeholder':'Recomendaciones para la siguiente sesion'}),
            'Archivo':forms.FileInput(attrs={'class':'form-control'}),
    }

And in the browser like this:

I would like you to help me find out how I can download this file through a link, as the following image shows:

    
asked by Evelyn Valeria 11.05.2016 в 03:26
source

1 answer

2

You must have a field in your model to save the file

class MyModel(models.Model):
    file = models.FileField(upload_to='subcarpeta/')

In the settings.py you must set these variables:

MEDIA_ROOT = '<your_path>/media/'
MEDIA_URL = '/media/'

Modify the urls.py to work in the development environment:

...
if settings.DEBUG is True:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

When the file is saved through your ModelForm, in your template you only have to show the field (which contains the url) in a link:

<a href="#">#                                    
answered by 11.05.2016 / 20:54
source