Saturday, May 21, 2016

Re: Displaying a document

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
)



On Saturday, May 21, 2016 at 11:07:11 AM UTC-7, luisza14 wrote:
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>






2016-05-21 10:43 GMT-06:00 Wilfredo Rivera <wriv...@gmail.com>:
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.

--
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


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/cc48ea36-6be4-419f-b5bc-c061399cec9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment