Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/media/Profile_pics/itachi.jpg

261 Views Asked by At

I am trying to load profile picture for each user default profile picture for user is default.jpg But happening is i am unable to load profile picture Page Not found Error occurs.

After running my program the media directory created in my project file with Profile_picture directory and i saved my default.jpg in media and uploaded profile picture are saved in Profile_picture

models.py:

class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        profile_picture = models.ImageField(default='default.jpg',upload_to='Profile_pics')

settings.py

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

urls.py

from django.contrib import admin
from django.urls import path,include
from users import views as user_view
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('',include('CovidHelp.urls')),
    path('profile/',include('users.urls')),
    path('register/',user_view.register,name='register'),
    path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),name='login'),
    path('logout/',auth_views.LogoutView.as_view(template_name='users/logout.html'),name='logout'),
    path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The Error message:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/media/Profile_pics/itachi.jpg
Using the URLconf defined in MyCovidHelp.urls, Django tried these URL patterns, in this order:

[name='Home']
about/ [name='About']
profile/
register/ [name='register']
login/ [name='login']
logout/ [name='logout']
admin/
^static/(?P<path>.*)$
The current path, media/Profile_pics/itachi.jpg, didn't match any of these.
1

There are 1 best solutions below

0
On

last line of urlpatterns in urls.py should be like this

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