H.fate H.fate - 2 months ago 21
Android Question

Getting image from Django on AWS, to Android Glide. 404 error

I implemented REST API using Django-rest-framework, deployed with AWS-ec2.

settings.py

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


serializers.py

class PostSerializer(serializers.ModelSerializer):
author = serializers.ReadOnlyField(source='author.username')
class Meta:
model = Post
fields = ('author', 'text', 'image')
def create(self, validated_data):
validated_data['author'] = self.context['request'].user
return super(PostSerializer, self).create(validated_data)


urls.py

router = SimpleRouter()
router.register(r'posts', views.PostViewSet, base_name='posts')
urlpatterns = router.urls

urlpatterns = [
url(r'^', include(router.urls)),
]


views.py

class PostViewSet(viewsets.ModelViewSet):
serializer_class = PostSerializer
permission_classes = [IsAuthenticated]
queryset = Post.objects.all()


So, it returned:

{
"author": "ghdalsrn",
"text": "11",
"image": "http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/images/images/Avril_Lavigne_-_00_-_Sk8er_Girl_-_Frontcover_-simplemp3s.jpg"
}


I used
"image": "http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/images/images/Avril_Lavigne_-_00_-_Sk8er_Girl_-_Frontcover_-simplemp3s.jpg"
on my Android studio.

Android MainActivity.java

public class MainActivity extends AppCompatActivity {

public ImageView mimage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mimage = (ImageView) findViewById(R.id.iv_image);

Glide.with(this)
.load("http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/images/images/Avril_Lavigne_-_00_-_Sk8er_Girl_-_Frontcover_-simplemp3s.jpg")
.centerCrop()
.into(mimage);


But, according to my putty message,

putty

"GET /images/images/filename_sk8er_Girl.jpg HTTP/1.1" 404 5184


Also my Android application doesn't show any image.

How do I fix this ?

Answer

Your folder structure in your server is wrong. According to this you should put your static files under a structure like this:

[home folder where manage.py is located]/[module_name]/[images, defined in settings.py]/[module_name]/[image_file_name]

In your case:

New/MyModule/images/MyModule/Avril_Lavigne_-_00_‌​-_Sk8er_Girl_-_Front‌​cover_-simplemp3s.jp‌​g

Double check your folder structures. You may have to update your file paths in your database.