Displaying a document

64 views
Skip to first unread message

Wilfredo Rivera

unread,
May 21, 2016, 12:54:57 PM5/21/16
to Django users
Hello:

I want to display an uploaded file in the browser. My website save the file in the database, but i don't have idea of how can the website display it once the user click on the link. I am new to django and programming in general so i am learning on a trial and error basis.

The view that handles this is the following:

def File_Open(request):
   
if (request == request.FILES):
       file
= request.FILES.open()
       
return render(request, 'recruitment/open_file.html', {file : 'file'})

and the url is this:

url(r'^candidatelist/'
        r
'resume-view/'
        r
'(?P<resumeFile>/$',
        views
.File_Open,
        name
= 'resume_view'

I have search for how can i do this, but haven't found a satisfying answer. Please i will appreciate the help.

Luis Zárate

unread,
May 21, 2016, 2:07:11 PM5/21/16
to django...@googlegroups.com
Do you have a model  with a fileField?


I think you are wrong passing file to template, if  you have a model with filefield you can use media url something like

class Mymodel(Model):
      myfile = models.FileField(upload_to="my_file_path_in_media")


so you can use a form

class Myform(forms.ModelForm):

     class Meta:
             model=Mymodel
             fields = '__all__'

so in the view you have

if request.method=='POST':
       form = MyForm(request.POST, request.FILE)
       if form.is_valid():
            instance=form.save()


so you can pass instance in template context

       render(request, 'mytemplate.html', {'instance': instance}

in your template you can put a link or whatever you want.

<a href="{{instance.myfile.url}}"> download</a>






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b2cf38b0-6a6a-45c1-84b9-c4cf678b6c9d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
"La utopía sirve para caminar" Fernando Birri


Wilfredo Rivera

unread,
May 21, 2016, 3:21:49 PM5/21/16
to Django users
I think my code is more or less what you said, i made the change in the template to include a link to the uploaded file using the Media_URL variable, but it didn't work. It gave me a page not found error. I don't know if i have to include a URL, the documentation doesn't mention anything. Below is my code if you can please check it out and tell me what i am missing.

This is my URL Conf:

 url(r'^candidatelist/'
        r
'(?P<year>\d{4})/'
        r
'(?P<month>\d{1,2})/'
        r
'(?P<day>\d{1,2})/'
        r
'(?P<slug>[-\w]*)/'
        r
'update/'
        r
'resume/'
        r
'(?P<resumeFile>)/$',
       
Candidate_Update.as_view(),
        name
= 'resume_view')



This is my model:

class Candidate(models.Model):
   
    resumeFile
= models.FileField(upload_to="resume/")
   



This is my template:


       
{% for candidate in candidatelist %}
       
<tr>
           
<td><a href="{{candidate.get_update_url}}">{{candidate.lastname}}</a>
           
</td>
            <td>{{candidate.firstname}}</
td>
           
<td>{{candidate.career}}</td>
           
<td>{{candidate.email}}</td>
           
<td><a href="{{ MEDIA_URL }}">{{candidate.resumeFile}}"</a></td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
   
   
</div>

{% endblock %}



and this is my view:
def CandidateList(request):
    candidatelist
= Candidate.objects.all()
   
return render(request, 'recruitment/candidatelist.html', {'candidatelist': candidatelist})

           
class Candidate_Detail(ObjectUpdateMixin, View):
    form_class
= CandidateForm
    model
= Candidate
    template_name
= 'recruitment/candidate_detail.html'


class Candidate_Update(ObjectUpdateMixin, View):
    form_class
= CandidateForm
    model
= Candidate
    template_name
= 'recruitment/candidate_update.html'



with the mixin:

class ObjectCreateMixin:
    form_class
= None
    template_name
= ' '

   
def get(self, request):
       
return render(
                      request
,
                     
self.template_name,
                     
{'form': self.form_class()}
                     
)
   
   
def post(self, request):
        bound_form
= self.form_class(request.POST, request.FILES)
       
if bound_form.is_valid():
            new_object
= bound_form.save()
           
return redirect(new_object)
       
else:
           
return render(
                          request
,
                         
self.template_name,
                         
{'form' : bound_form})
           

class ObjectUpdateMixin:
    form_class
= None
    model
= None
    template_name
= ' '
   
   
def get(self, request, slug, **kwargs):
        obj
= get_object_or_404(self.model,
                                 slug__iexact
=slug
                                         
)
       
        context
= {'form' : self.form_class(instance=obj),
                   
self.model.__name__.lower() : obj,}
       
return render(
                      request
,
                     
self.template_name,
                      context
                     
)
   
   
def post(self, request, slug, **kwargs):
        obj
= get_object_or_404(self.model, slug=slug)
        bound_form
= self.form_class(request.POST, request.FILES, instance=obj)
       
if bound_form.is_valid():
            new_object
= bound_form.save()
           
return redirect(new_object)
       
else:
            context
= {
                       
'form' : bound_form,
                       
self.model.__name__.lower() : obj,
                       
}
           
           
return render(
                          request
,
                         
self.template_name,
                          context
)

Luis Zárate

unread,
May 21, 2016, 10:56:42 PM5/21/16
to django...@googlegroups.com

2016-05-21 13:21 GMT-06:00 Wilfredo Rivera <wriv...@gmail.com>:
<td><a href="{{ MEDIA_URL }}">{{candidate.resumeFile}}"</a></td>


<td><a href="{{candidate.resumeFile.url}}">{{candidate.resumeFile.name}}"</a></td>

Looking in your code probably you could be interested in https://docs.djangoproject.com/ja/1.9/ref/class-based-views/generic-editing/


Wilfredo Rivera

unread,
May 22, 2016, 11:27:10 AM5/22/16
to Django users
It give me this kind of error:


Using the URLconf defined in hr_solution.urls, Django tried these URL patterns, in this order:

  1. ^admin/
  2. ^user/
  3. ^$ [name='candidate_entry']
  4. ^candidatelist/$ [name='candidate_list']
  5. ^candidatelist/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]*)/$ [name='candidate_detail']
  6. ^candidatelist/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]*)/update/$ [name='candidate_update']

The current URL, candidatelist/resume/Resume_Wilfredo_Rivera.pdf, didn't match any of these.


How can I desing a url that match the one with the uploaded file and what view handles it?

Wilfredo Rivera

unread,
May 22, 2016, 12:36:15 PM5/22/16
to Django users
I fixed it. but now the website give me the option of downloading the file. How can i do for the website to display the file?

Derek

unread,
May 23, 2016, 5:00:27 AM5/23/16
to Django users
The users will need a browser which is capable of rendering the file or knowing what app it has to use - for example, on my machine (Linux) an XLS file is rendered directly by LibreOffice; but on a colleague's machine with Windows/IE they have to first download the file and then open it. So, you will need to provide instructions for users to configure their browsers.
Reply all
Reply to author
Forward
0 new messages