Dragon.M
Dragon.M

Reputation: 277

How to set image upload root for django application

I have trouble of setting up the root folder for uploaded images. The upload image is inside django application named api.

The view returns this link on image: http://www.example.com/media/4a273bdf-564d-4dec-b657-43db27f04042.jpeg

However when I go on that link I get error because image is not on there.

I set in settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAdminUser',
    ],
    'PAGE_SIZE': 1
}

MEDIA_ROOT = os.path.join(BASE_DIR,STATIC_ROOT,'media')
MEDIA_URL = '/media/'

and I write this in api/models.py:

from django.db import models
import uuid

def scramble_uploaded_filename(instance, filename):
    extension = filename.split(".")[-1]
    return "{}.{}".format(uuid.uuid4(), extension)

def filename(instance, filename):
    return filename

# Create your models here.
class UploadImage(models.Model):
    image = models.ImageField("Uploaded image",upload_to=scramble_uploaded_filename)
    captcha_type = models.IntegerField("Image type")

views.py

from rest_framework import viewsets
from api.models import UploadImage
from api.serializers import UploadedImageSerializer
from rest_framework import authentication, permissions
from rest_framework.parsers import MultiPartParser,FormParser
import MySQLdb

class FileUploadViewSet(viewsets.ModelViewSet):
    #create queryset view
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UploadImage.objects.filter(id=1,user='auth.User')
    serializer_class = UploadedImageSerializer
    parser_classes = (MultiPartParser, FormParser,)

serializers.py

from rest_framework import serializers
from api.models import UploadImage # Import our UploadedImage model

class UploadedImageSerializer(serializers.ModelSerializer):
    # current_user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    class Meta:
        model = UploadImage
        fields = ('id', 'image','captcha_type','created','user','result') #

any ideas?

Upvotes: 1

Views: 678

Answers (2)

rlfrahm
rlfrahm

Reputation: 359

Assuming (based on your comments below the OP) that you are trying to deploy this to a remote server:

The first thing to check would be that your image that you uploaded in fact exists at the path [project root]/media/4a273bdf-564d-4dec-b657-43db27f04042.jpeg.

The second thing to check would be that either Nginx or Apache or [insert web server] is properly routing requests to your media directory to the right location on your server.

EDIT #1

This is an example location block for your Nginx configuration file that routes requests from the url www.example.com/media/filename_of_image.jpeg to /absolute/path/to/media/:

location /media/  {
       alias /absolute/path/to/media/;
}

This should be placed within a server block, before your location block that defines the path /.

Upvotes: 1

Marine Fighter
Marine Fighter

Reputation: 403

Hi m8 all you have to do is edit urls.py add this lines and it should work.

from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
   url(r'^admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 3

Related Questions