Upload above apache web root?

105 views
Skip to first unread message

Federico Capoano

unread,
Sep 29, 2010, 1:46:06 PM9/29/10
to Django users
Hi everyone,

is it possible to write a custom model filefield that upload files
somewhere above the public directory?

Federico Capoano

unread,
Sep 30, 2010, 3:49:29 AM9/30/10
to Django users
Is this not possible? I wonder why Django force to upload files only
in a certain directory and doesn't allow more flexibility.

blaamann

unread,
Sep 30, 2010, 4:05:32 AM9/30/10
to Django users
I haven't tried this myself, but a custom file storage might help you:
http://docs.djangoproject.com/en/dev/howto/custom-file-storage/

or using a callback:
http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield

Regards

Xavier Ordoquy

unread,
Sep 30, 2010, 4:10:25 AM9/30/10
to django...@googlegroups.com
Well, the documentation already tells you about it:
http://docs.djangoproject.com/en/1.2/topics/files/#the-built-in-filesystem-storage-class

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Federico Capoano

unread,
Sep 30, 2010, 4:35:07 AM9/30/10
to Django users
Yes you're right, I'm trying it out. I'll post a solution when I'm
done.


On 30 Set, 10:10, Xavier Ordoquy <xordo...@linovia.com> wrote:
> Well, the documentation already tells you about it:http://docs.djangoproject.com/en/1.2/topics/files/#the-built-in-files...

Federico Capoano

unread,
Sep 30, 2010, 4:37:51 AM9/30/10
to Django users
PS: i'll post a solution so other noobs like me will see how to do
that..

Xavier Ordoquy

unread,
Sep 30, 2010, 4:40:54 AM9/30/10
to django...@googlegroups.com
Hi,

To me, it looks like the documentation already gave the answer.

Regards,
Xavier.

Federico Capoano

unread,
Sep 30, 2010, 12:45:42 PM9/30/10
to Django users
I wanted to write this post anyway, gives a quick solution to upload
files above the public directory and serve the files with django-
filetransfers. Hopefully it will be easier to find from search
engines.

http://nemesisdesign.net/blog/coding/django-private-file-upload-and-serving/

Stephanie Socias

unread,
Apr 6, 2015, 5:38:36 PM4/6/15
to django...@googlegroups.com
I've implemented this same solution (using custom file storage and the "upload_to" parameter) but now cannot get the uploaded files to display from my template. I would normally use {{ STATIC_URL }} but, since I've now specified a custom location, I'm not sure why my path, which I've hard-coded in the template, isn't working...any ideas?

fs = FileSystemStorage(location='/data/www/upload')

def image_location(instance, filename):
    return '/'.join(['thumbnails', str(instance.dataset_id), filename])

class DataModel(models.Model):
    name = models.CharField()
    date = models.DateField(blank=True)  
    image = models.ImageField(storage=fs, upload_to=image_location, blank=True)

template.html

<a href="{% url 'myproj:items' %}">
    <img class="media-object img-thumbnail" src="/data/www/upload/{{ datamine.image }}" alt="Data Image Here" height="130" width="130">
</a>

Daniel Roseman

unread,
Apr 7, 2015, 5:36:37 AM4/7/15
to django...@googlegroups.com
On Monday, 6 April 2015 22:38:36 UTC+1, Stephanie Socias wrote:
I've implemented this same solution (using custom file storage and the "upload_to" parameter) but now cannot get the uploaded files to display from my template. I would normally use {{ STATIC_URL }} but, since I've now specified a custom location, I'm not sure why my path, which I've hard-coded in the template, isn't working...any ideas?

fs = FileSystemStorage(location='/data/www/upload')

def image_location(instance, filename):
    return '/'.join(['thumbnails', str(instance.dataset_id), filename])

class DataModel(models.Model):
    name = models.CharField()
    date = models.DateField(blank=True)  
    image = models.ImageField(storage=fs, upload_to=image_location, blank=True)

template.html

<a href="{% url 'myproj:items' %}">
    <img class="media-object img-thumbnail" src="/data/www/upload/{{ datamine.image }}" alt="Data Image Here" height="130" width="130">
</a>

But /data/www/upload/... is the file path of your upload, not the URL. You still need an actual URL that is served by your webserver. Obviously, a browser can't access arbitrary file paths on your server.
--
DR. 

Stephanie Socias

unread,
Apr 7, 2015, 9:51:56 AM4/7/15
to django...@googlegroups.com
Thank you very much for responding, Daniel.

I'm sorry I don't quite follow- I've never done this before. I should use the URL {{ datamine.image.url }} in the template?

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/aHa0WWHGreY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.

Stephanie Socias

unread,
Apr 7, 2015, 11:05:34 AM4/7/15
to django...@googlegroups.com
Do I need to update/add something to my urls.py?


On Tuesday, April 7, 2015 at 9:51:56 AM UTC-4, Stephanie Socias wrote:
Thank you very much for responding, Daniel.

I'm sorry I don't quite follow- I've never done this before. I should use the URL {{ datamine.image.url }} in the template?
On Tue, Apr 7, 2015 at 5:36 AM, Daniel Roseman <dan...@roseman.org.uk> wrote:
On Monday, 6 April 2015 22:38:36 UTC+1, Stephanie Socias wrote:
I've implemented this same solution (using custom file storage and the "upload_to" parameter) but now cannot get the uploaded files to display from my template. I would normally use {{ STATIC_URL }} but, since I've now specified a custom location, I'm not sure why my path, which I've hard-coded in the template, isn't working...any ideas?

fs = FileSystemStorage(location='/data/www/upload')

def image_location(instance, filename):
    return '/'.join(['thumbnails', str(instance.dataset_id), filename])

class DataModel(models.Model):
    name = models.CharField()
    date = models.DateField(blank=True)  
    image = models.ImageField(storage=fs, upload_to=image_location, blank=True)

template.html

<a href="{% url 'myproj:items' %}">
    <img class="media-object img-thumbnail" src="/data/www/upload/{{ datamine.image }}" alt="Data Image Here" height="130" width="130">
</a>

But /data/www/upload/... is the file path of your upload, not the URL. You still need an actual URL that is served by your webserver. Obviously, a browser can't access arbitrary file paths on your server.
--
DR. 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/aHa0WWHGreY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

Luis Zárate

unread,
Apr 7, 2015, 12:31:00 PM4/7/15
to django...@googlegroups.com
Hi,

Django provide for development MEDIA_URL and MEDIA_ROOT for uploaded file, for production used static deploy strategy, see the documentation about static files.

Model file field has an url attribute that return a media url, you use in template like {{obj.file.url}}
>>> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e14088d0-7c4a-4965-b88a-2fd0b26ec3cb%40googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b015d26d-c95a-4435-bd93-107244d5a798%40googlegroups.com.

> For more options, visit https://groups.google.com/d/optout.
>

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



Stephanie Socias

unread,
Apr 7, 2015, 12:47:56 PM4/7/15
to django...@googlegroups.com
Hi Luis,

Yes, I used {{ datamine.image.url }} in my template but it doesn't work. I'm not sure how to format my urls.py since I haven't changed the MEDIA_URL and MEDIA_ROOT. I want to upload these images to a location not in my MEDIA_ROOT...?

This is my urls.py:
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tt.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('myproj.urls', namespace='myproj')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Luis Zárate

unread,
Apr 7, 2015, 1:47:01 PM4/7/15
to django...@googlegroups.com
You are not set base_url in FileSystemStorage so you are using MEDIA_URL by
default.

The imagefield url property is called like this

def _get_url(self):
self._require_file() # check if It is an object
return self.storage.url(self.name)
url = property(_get_url)
And self.storage.url throw exception if base_url is not set.

def url(self, name):
if self.base_url is None:
raise ValueError("This file is not accessible via a URL.")
return urljoin(self.base_url, filepath_to_uri(name))

So, I should try to call datamine.image.url() in shell and see what happen 


For more options, visit https://groups.google.com/d/optout.

Stephanie Socias

unread,
Apr 7, 2015, 5:01:20 PM4/7/15
to django...@googlegroups.com
Thank you all for your input. It turns out my settings file was improperly configured. I needed to change MEDIA_ROOT and MEDIA_URL

On Tue, Apr 7, 2015 at 12:30 PM, Luis Zárate <luis...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages