On Monday 17 August 2009 01:10:33 pm Fluoborate wrote:
> Hello All,
>
> I am being very perplexed and annoyed by models.FileField database
> fields. I have two problems:
>
> 1. The filefield.url attribute gives the path on my hard disk, not the
> internet URL that the file is being served to. The filefield.path and
> filefield.name attributes also give the exact same result as .url.
>
> 2. How am I supposed to make a form where the user uploads something?
> I think I can hack something together by reverse-engineering the Admin
> page, and by looking around on the internet, but I don't see one good
> and simple example. I want simple.
>
> My setup:
>
> Mac OS X 10.5.8
> Django 1.0.2
> Development server only, at the moment.
>
> #In settings.py:
> MEDIA_URL = '/media/'
> MEDIA_ROOT = '/Users/me/publications/'
>
> #In models.py:
> class Publication(models.Model):
>         pdf = models.FileField(upload_to='pdf')
>
> #In urls.py:
> urlpatterns += patterns('',
>        (r'^media/(?P<path>.*)$', 'django.views.static.serve',
> {'document_root': '/Users/me/publications'}),
> )
>
> I think that's all the relevant stuff. Some findings/example tasks:
>
> I upload a file, essay.pdf, using the Django Admin console.
> http://127.0.0.1:8000/media/pdf/essay.pdf
> #It works, there it is.
>
> In the shell, (python manage.py shell):
> >>>essay.pdf.path
>
> u'/Users/me/publications/pdf/essay.pdf'
>
> >>>essay.pdf.name
>
> u'/Users/me/publications/pdf/essay.pdf'
>
> >>>essay.pdf.url
>
> u'/Users/me/publications/pdf/essay.pdf'
>
> Now that doesn't seem quite right. I have seen working examples, I
> even made one once, I don't know what is wrong here.
>

If you're using the admin as the only way to upload files, I do this in one of 
my apps:

By customizing the save_model[1] to handle the upload of files, write the 
files to the savel location, then return the relative path for the files 
under my  MEDIA_ROOT, and save it to the imagefile field. 

class ImageAdmin(admin.ModelAdmin):
  list_display = ['name', 'gallery', 'date' ]
  search_fields = ['name', 'gallery__name']  
  prepopulated_fields = {'slug': ("name",), }
  list_filter = ['gallery', ]                

  def save_model(self, request, obj, form, change):
    def uploadHandler(f, saveLocation):
      savePath = os.path.join(settings.MEDIA_ROOT, saveLocation, f.name)
      destination = open(savePath, 'w')
      for chunk in f.chunks():
        destination.write(chunk)
      destination.close()
      return os.path.join(saveLocation, f.name)
    if request.FILES.has_key('imagefile'):
      obj.imagefile = uploadHandler(request.FILES['imagefile'], 
settings.GALLERY_SAVE_LOCATION)
    obj.save()

admin.site.register(ImageFile, ImageAdmin)

The FileFields upload_to[1] option can also take a callable to handle the file 
fields location, this uploadHandler can be placed there for a more robust 
application.  

The keys are write the file by reading the chunks[3] to the place you really 
want it stored and then return the relative path  that's under the 
MEDIA_ROOT, then use the MEDIA_URL context processor[5] in your templates 
like <a href="{{ MEDIA_URL }}{{ myfile.path }}"> myfile.name</a>  

Mike

[1] http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-methods
[2] http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
[3] 
http://docs.djangoproject.com/en/dev/topics/http/file-uploads/#handling-uploaded-files
[4] 
http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-media
-- 
Making one brilliant decision and a whole bunch of mediocre ones isn't as
good as making a whole bunch of generally smart decisions throughout the
whole process.
        -- John Carmack

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to