OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Django adding leading slash when saving the path

  • Thread starter Thread starter Akari Oozora
  • Start date Start date
A

Akari Oozora

Guest
I'm creating a route to save some photos and, for now, they're being saved on my desktop. This can be easily changed on my .env file:

Code:
MEDIA_URL=C://Users/User/Desktop/

Django saves the file correctly and it can be seen on its location as expected. However, on the database, the file is saved with an extra leading slash

Code:
/C:/Users/User/Desktop/photo.png

I know this is kinda a silly question, since this slash may be removed on the application, by manually editing before saving it, etc. But I'd like to know if there's a built-in solution in Django for this

My settings.py variables are the following:

Code:
MEDIA_URL = os.environ.get('MEDIA_URL')
MEDIA_ROOT = os.environ.get('MEDIA_URL')

If you need my view, here it is:

Code:
@api_view(['PATCH'])
def updatePhoto(request):
    user= request.user

    if 'photo' not in request.FILES:
        return Response({'message': 'Error', 'error': 'No photo uploaded'}, status=400)

    try:
        photo = request.FILES['photo']
        fs = FileSystemStorage()
        filename = fs.save(name=photo .name, content=photo )

        uploaded_file_url = fs.url(filename)

        user.foto_url = uploaded_file_url
        user.save()

        return Response({'message': 'Updated'}, status=200)
    except Exception as e:
        return Response({'message': 'Error', 'error': str(e)}, status=500)
<p>I'm creating a route to save some photos and, for now, they're being saved on my desktop. This can be easily changed on my <code>.env</code> file:</p>
<pre><code>MEDIA_URL=C://Users/User/Desktop/
</code></pre>
<p>Django saves the file correctly and it can be seen on its location as expected. However, on the database, the file is saved with an extra leading slash</p>
<pre><code>/C:/Users/User/Desktop/photo.png
</code></pre>
<p>I know this is kinda a silly question, since this slash may be removed on the application, by manually editing before saving it, etc. But I'd like to know if there's a built-in solution in Django for this</p>
<p>My <code>settings.py</code> variables are the following:</p>
<pre><code>MEDIA_URL = os.environ.get('MEDIA_URL')
MEDIA_ROOT = os.environ.get('MEDIA_URL')
</code></pre>
<p>If you need my view, here it is:</p>
<pre><code>@api_view(['PATCH'])
def updatePhoto(request):
user= request.user

if 'photo' not in request.FILES:
return Response({'message': 'Error', 'error': 'No photo uploaded'}, status=400)

try:
photo = request.FILES['photo']
fs = FileSystemStorage()
filename = fs.save(name=photo .name, content=photo )

uploaded_file_url = fs.url(filename)

user.foto_url = uploaded_file_url
user.save()

return Response({'message': 'Updated'}, status=200)
except Exception as e:
return Response({'message': 'Error', 'error': str(e)}, status=500)
</code></pre>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top