Pillow upload path in pythonanewhere? - django

I'm using pythonanywhere.com and would like to attach images for my posts. When installed my model, I have a folder path for downloads like this
image = models.ImageField(upload_to="/static/uploads/",
but when I attach the photo I receive a message
The joined path (/static/uploads/1.jpg) is located outside of the base path component (/home/farmville)
if I specify the full path
/home/username/project/static/uploads/
it's working, but in the template file this path does not work, the image is not :(
How can it can be solved?

The two paths are different things, so using the same path doesn't make sense. The upload_to path is a path to a directory on the server where the image should be uploaded. The path in the template is probably a URL path (it's hard to tell without the actual template) and so it needs to point to where you're serving the uploads from.

Related

How to get the path of any folder I want when I select them in django

I want the user to be able to get the path of any directory so that my code can handle the DICOM images in that directory. Is there any way? I tried using tkinter but its window is always showing out before I get on my web.

django-ckeditor change upload path prefix

I want to change the prefix of upload path in django-ckeditor. By default it generates subdirectories by using username and date so something like:
/media/uploads/username/year/month/day/uploaded_file
The documentation says:
Set the CKEDITOR_RESTRICT_BY_USER setting to True in the project’s settings.py file (default False). This restricts access to uploaded images to the uploading user (e.g. each user only sees and uploads their own images). Upload paths are prefixed by the string returned by get_username. If CKEDITOR_RESTRICT_BY_USER is set to a string, the named property is used instead.
After few tries, still can't figure out how to configure this to handle prefix of uploaded files.
Thanks for any help.
Just set CKEDITOR_RESTRICT_BY_USER = True in your settings.py file.
The upload path will be /media/uploads/username/year/month/day/uploaded_file as you desired.
Ckeditor processes the user path using following view. https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor_uploader/views.py

Set up two media directories

I am doing Django project. I installed django-watermark from PyPI to make watermarks on my pictures.
Here you see my media directory,
when pictures are uploaded to django, they appear in Media root. Next, watermark library grabs those pictures, adds watermarks and drops them to "watermarked" folder. Eventually, pictures have to be fetched from "watermarked" directory.
Website works perfectly when debug=True, however on my server (I use AWS, IIS Windows for hosting), when I set debug=False, instead of pictures I get 404 error.
My virtual directory for IIS is set to be my Media root directory
these are my settings and url files
After 48 hours of research I finally found the solution. There is no issue with my settings.py or the way I have configured Media_root or URL, nor there is need for usage of os.path.join or anything else....
It was all about IIS, even though I have had configured virtual directories, I haven't configured their handlers. Apparently, I have had to open virtual directory for media files --> double click on "handlers" --> View ordered list --> move "static" at the top of the list, that was all.

Get absolute file path of FileField of a model instance in Django

I have a page where people can upload files to my server.
I want to do something with the file in Celery. So, I need to know the absolute filepath of the uploaded FileFiled of my Model.
Let's say I queried the model and got the instance.
Now I need to get the absolute file path including the filepath.
obj = Audio.objects.get(pk=1)
I'm currently trying obj.filename and it's only printing the file name and not the absolute path.
I know I can get the upload path I input into upload_to and media directory, but I was wondering if there was a more DRY and automatic approach.
How do I get the absolute path of file which is a file filed in obj?
Found an answer.
I gotta do a .path on the FileField
If I do
obj.audio_file.path
obj is the model instance I queried and audio_file is the filefield

Django How to allow apache to create directories?

I am making a django application that makes user of django's ImageField to upload a file to a specific folder. I am using this field for storing the user's profile pictures. But the problem is the path that I give to upload_to is dynamic and depends on user and will create directories if needed. i.e if the path is user/1/profile-pic/large/pic.jpg, it will create the directores, user/, user/1/ so on, if the are not already there. It worked fine in development. But now when I have put my website on a VM and serving it using apache. Django raises the permission denied error. As I have to make directories dynamically so I can't make them ahead of everything and change their permissions. So I was wondering if there is any of way of acoomplishing it.
You should chown you media directory to the user which runs you django app.