0

my friends, this is my first question here. So, I follow the Django documentation and some questions here, but the problem keep happening. I did what was said on other similar questions, like this one, for examaple: Issue with image in django But the problem persists.

So, my model looks like this:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key=True)
    picture = models.ImageField(blank = True, null = True, upload_to = user_directory_path)
    whatsapp = models.PositiveIntegerField(blank = True, null = True)

My settings looks like this:

MEDIA_ROOT = f"{BASE_DIR}/media"
MEDIA_URL = '/media/'

I added

+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

to the end of my urls.py file:

from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .views import UserList, FeedbackList, DayList, AttractionList, UserDetail, UserProfileList, ProfileDetail
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

urlpatterns = [
    path('users/', UserList.as_view()),
    path('user/<str:username>', UserDetail.as_view()),
    path('profiles/', UserProfileList.as_view()),
    path('profile/<str:user__username>', ProfileDetail.as_view()),
    path('feedbacks/', FeedbackList.as_view()),
    path('days/', DayList.as_view()),
    path('attractions/', AttractionList.as_view()),
    path('token/', TokenObtainPairView.as_view()),
    path('token/refresh/', TokenRefreshView.as_view()),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

The folder and images are being created on my file system when I add them via the admin, but when I click to see them over there I recieve a 404.

admin screen

error screen

Request URL:    http://192.168.15.21:8000/media/uploads/profile_pictures/1-417ee0cb-cd83-4ea6-a692-a4af3b4afcce-eu.jpg

Using the URLconf defined in evento.urls, Django tried these URL patterns, in this order:

event_app/
admin/
The current path, media/uploads/profile_pictures/1-417ee0cb-cd83-4ea6-a692-a4af3b4afcce-eu.jpg, didn’t match any of these.

This is my file structure

folder

I am making a mobile app and serving my data using Django Rest Framework, the images are the only thing giving me a headache right now.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • Can you send me photo of your image folder – Parth Mehta Jan 13 '23 at 11:40
  • Please show full main urls.py. Error is clearly saying that you don't have appropriate url pattern. Also: the error message is a **text** so please post it as **test** not as picture. Your screenshot of admin page contains specific media file url which would also be helpful to resolve your issue because dispatching media files performs mapping of urls to paths. This URL is also **text** so please post it as **text** not as picture. – Ivan Starostin Jan 13 '23 at 11:57
  • [folder] (https://imgur.com/a/84nCpTU) – Cleiton Oliveira Jan 13 '23 at 11:57
  • 1
    Add `static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)` to your **Project's** `urls.py` file. – Abdul Aziz Barkat Jan 13 '23 at 12:06
  • This is not your main urls.py. What you showed is an app's urls.py - event_app/urls.py. Your main urls.py is in `evento` folder. – Ivan Starostin Jan 13 '23 at 12:12
  • Can you tell us what `user_directory_path` is? – Adil Mohak Jan 13 '23 at 14:11

1 Answers1

0

Herer i have change your upload_to = profile_pictures

 class UserProfile(models.Model):
        user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key=True)
        picture = models.ImageField(blank = True, null = True, upload_to = profile_pictures)
        whatsapp = models.PositiveIntegerField(blank = True, null = True)

I have also change settings as given below

MEDIA_URL = '/media/'

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

In your main project app you can add urlpatterns as given below if you have to

urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
Parth Mehta
  • 191
  • 6