I wrote the code to update user image (default as well as uploaded) on the user profile page. However, it’s not working and just showing some icon instead of the real image. Please can someone help to fix the issue?
In user app:
Model.py as follows:
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default=‘profilepic.jpg’, upload_to=‘profile_pictures’)
location = models.CharField(max_length=100)
def str(self):
return self.user.username
In main app (root) settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR,‘pictures’)
MEDIA_URL = ‘/pictures/’
In root app urls.py
from django.contrib import admin
from django.urls import path, include
from users import views as user_views
from django.contrib.auth import views as authentication_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘food/’, include(‘food.urls’)),
path(‘register/’, user_views.register, name=‘register’),
path(‘login/’, authentication_views.LoginView.as_view(template_name=‘users/login.html’), name=‘login’),
path(‘logout/’, authentication_views.LogoutView.as_view(template_name=‘users/logout.html’), name=‘logout’),
path(‘profile/’, user_views.profilepage, name=‘profile’),
]
urlpatterns += [
# … the rest of your URLconf goes here …
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)