Andres Vargas - zodman

Mostrar file widget del admin en el form input de template

from django import forms from django.contrib.admin.widgets import AdminFileWidget

class AgregarGuiaForm(forms.Form): year = forms.CharField() file = forms.FileField(widget=AdminFileWidget)

#views.py

<span style="color: #0000aa">try</span>:
    <span style="color: #00aaaa">file</span> = guide.get_file().file
    <span style="color: #0000aa">try</span>:
        f = SimpleUploadedFile(<span style="color: #00aaaa">file</span>.name, <span style="color: #00aaaa">file</span>.read())
        f.url = <span style="color: #00aaaa">file</span>.url
        file_data = {<span style="color: #aa5500">&#39;file&#39;</span>: f}
    <span style="color: #0000aa">except</span>:
        messages.error(request,<span style="color: #aa5500">&quot;archivo no existe %s&quot;</span> % <span style="color: #00aaaa">file</span>.url)
        file_data = {}
<span style="color: #0000aa">except</span>:
    messages.error(request,<span style="color: #aa5500">&quot;No existe archivo para esta guia&quot;</span>)
    
form1 = AgregarGuiaForm({<span style="color: #aa5500">&#39;year&#39;</span>: guide.year}, file_data)

#output: http://img225.imageshack.us/img225/6347/screenshot1qz.png

#Linux #Python #Django #Planetalinux