Home > other >  Django display Image of User
Django display Image of User

Time:01-25

Good Day, every user has an image, how can I display the specific user image?

\\models.py

class UserImage(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, default=None, null=True, on_delete=models.CASCADE)
    photo = models.ImageField(
        upload_to='images/', height_field=None, width_field=None, max_length=100)

    def __str__(self):
        return str(self.user)

\\forms.py

class UserImageForm(forms.ModelForm):
    class Meta:
        model = UserImage

        fields = (
            'photo',
        )

\\views.py

def photo_view(request):
    try:
        profile = request.user.userimage
    except UserImage.DoesNotExist:
        profile = UserImage(user=request.user)

    if request.method == 'POST':
        form = UserImageForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():
            form.save()
            return redirect('/dashboard/user/photo')
    else:
        form = UserImageForm(instance=profile)

    return render(request, 'photo.html', {'form': form, 'profile': profile})

test.html

<img src="{{ WhatHere? }}" alt="User Profile Image" />

So in src="" must be the URL to the image what the user has added. Each user can only see his own image, not all existing images should be displayed.

EDIT \\settings.py

LOGIN_URL = '/signin'
LOGIN_REDIRECT_URL = '/signin'

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

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

\\ urls.py

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from abstractuser.views import (
    signup_view,
    signin_view,
    signout_view,
)

from dashboard.views import (
    dashboard_home_view,
    title_delete,
    profile_view,
    photo_view,
)

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('signup/', signup_view),
    path('signin/', signin_view),
    path('signout/', signout_view),
    path('dashboard/home', dashboard_home_view),
    path('dashboard/title/delete/<int:title_id>',
         title_delete, name="delete_title"),
    path('dashboard/user/profile/', profile_view),
    path('dashboard/user/photo/', photo_view),
]

I think I have to add something to the urls.py but I have no idea what

CodePudding user response:

You can use

<img src="{{ profile.photo.url }}" alt="User Profile Image" />

And in your settings.py you have to set MEDIA_URL and MEDIA_ROOT AS

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

MEDIA_URL = '/media/'

And in your project's main urls.py

from django.conf import settings 
from django.conf.urls.static import static 


urlpatterns = [
    path('admin/', admin.site.urls),
    path('signup/', signup_view),
    path('signin/', signin_view),
    path('signout/', signout_view),
    path('dashboard/home', dashboard_home_view),
    path('dashboard/title/delete/<int:title_id>',
     title_delete, name="delete_title"),
    path('dashboard/user/profile/', profile_view),
    path('dashboard/user/photo/', photo_view),
]

urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  •  Tags:  
  • Related