How do I create an ‘Update Profile Picture’ function in django

Okay, so basically I created a function in django where you can change the profile picture but the issue is that It’s doing something else instead. Have a look below to understand what’s happening

I have added these codes into my settings.py file

settings.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
</code>
<code>STATIC_URL = 'static/' MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') </code>
STATIC_URL = 'static/'

MEDIA_URL = '/media/'

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

and also added these to the urls.py

urls.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from django.contrib import admin
from django.urls import path , include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('user/',include('members.urls')),
path('',include('blogposts.urls')), # MAIN URL
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
</code>
<code>from django.contrib import admin from django.urls import path , include from django.conf.urls.static import static from django.conf import settings urlpatterns = [ path('admin/', admin.site.urls), path('user/',include('members.urls')), path('',include('blogposts.urls')), # MAIN URL ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT) </code>
from django.contrib import admin
from django.urls import path , include 
from django.conf.urls.static import static
from django.conf import settings 

urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/',include('members.urls')),
    path('',include('blogposts.urls')), # MAIN URL 
    
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

This is the model that I created for the User

models.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Profile(models.Model):
user = models.OneToOneField(User,on_delete = models.CASCADE, null = True , blank = True )
bio = models.TextField()
profile_pic = models.ImageField(upload_to = 'images',default= '/../media/images/image.png')
def __str__(self):
return str(self.user)
</code>
<code>class Profile(models.Model): user = models.OneToOneField(User,on_delete = models.CASCADE, null = True , blank = True ) bio = models.TextField() profile_pic = models.ImageField(upload_to = 'images',default= '/../media/images/image.png') def __str__(self): return str(self.user) </code>
class Profile(models.Model):
    user = models.OneToOneField(User,on_delete = models.CASCADE, null = True , blank = True )
    bio = models.TextField()
    profile_pic = models.ImageField(upload_to = 'images',default= '/../media/images/image.png')

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

This is the form that I created to change the Profile Pic

forms.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['profile_pic']
</code>
<code>class ProfileForm(forms.ModelForm): class Meta: model = Profile fields = ['profile_pic'] </code>
class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile 
        fields = ['profile_pic']

This is the urls.py inside the app

urls.py (app)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from django.urls import path , include
from . import views
urlpatterns = [
path('',views.homepage,name = 'home-page'),
path('view_post/<int:pk>',views.view_post,name='view-post'),
path('blogcreation',views.create_blog,name='blog-form'),
path('aboutme',views.about_user,name = 'show-user'), # this is the one that takes to the page where you can change your profile picture and displays your bio
path('delete/<int:pk>',views.delete_post,name = 'delete-post'),
path('edit_blog/<int:pk>',views.edit_blog,name = 'edit-post'),
]
</code>
<code>from django.urls import path , include from . import views urlpatterns = [ path('',views.homepage,name = 'home-page'), path('view_post/<int:pk>',views.view_post,name='view-post'), path('blogcreation',views.create_blog,name='blog-form'), path('aboutme',views.about_user,name = 'show-user'), # this is the one that takes to the page where you can change your profile picture and displays your bio path('delete/<int:pk>',views.delete_post,name = 'delete-post'), path('edit_blog/<int:pk>',views.edit_blog,name = 'edit-post'), ] </code>
from django.urls import path , include 
from . import views 


urlpatterns = [
    path('',views.homepage,name = 'home-page'),
    path('view_post/<int:pk>',views.view_post,name='view-post'),
    path('blogcreation',views.create_blog,name='blog-form'),

    path('aboutme',views.about_user,name = 'show-user'), # this is the one that takes to the page where you can change your profile picture and displays your bio

    path('delete/<int:pk>',views.delete_post,name = 'delete-post'),
    path('edit_blog/<int:pk>',views.edit_blog,name = 'edit-post'),
]

These are the codes in views.py

views.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def about_user(request):
profile = Profile.objects.all()
if request.method == "POST":
form = ProfileForm(request.POST,request.FILES)
if form.is_valid():
form.save()
return render(request, 'show_user.html',{'form':form,'profile':profile})
else:
form = ProfileForm()
return render(request, 'show_user.html',{'form':form,'profile':profile})
</code>
<code>def about_user(request): profile = Profile.objects.all() if request.method == "POST": form = ProfileForm(request.POST,request.FILES) if form.is_valid(): form.save() return render(request, 'show_user.html',{'form':form,'profile':profile}) else: form = ProfileForm() return render(request, 'show_user.html',{'form':form,'profile':profile}) </code>
def about_user(request):

    profile = Profile.objects.all()

    if request.method == "POST":
        form = ProfileForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            return render(request, 'show_user.html',{'form':form,'profile':profile})

    else:
        form = ProfileForm()
    return render(request, 'show_user.html',{'form':form,'profile':profile})

and finally. This is show_user.html my html page.

show_user.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{{user.profile.bio}}
<img src = "{{user.profile.profile_pic}}" height = "500">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
</code>
<code>{{user.profile.bio}} <img src = "{{user.profile.profile_pic}}" height = "500"> <form method="POST" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} <button type="submit">Upload</button> </form> </code>
{{user.profile.bio}}
<img src = "{{user.profile.profile_pic}}" height = "500">



<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Upload</button>
</form>



and these are my directories.

Now the issue is that my user profile bio is working fine and my default user profile is showing.

But when I want to change the pic and click on ‘choose file’ and choose a random pic which is located in my media folder

after selecting ‘download.jpg’ and uploading it. This is what happens.

any solutions ?

I cannot wrap my head around this and I can’t find any solutions.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật