Skip to content

Commit b2c0598

Browse files
author
mominur-helios
committed
separate avatar upload
1 parent b3764d7 commit b2c0598

File tree

5 files changed

+33
-75
lines changed

5 files changed

+33
-75
lines changed

apps/users/forms.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,12 @@ class UserPasswordChangeForm(PasswordChangeForm):
7272
class ProfileForm(forms.ModelForm):
7373
class Meta:
7474
model = Profile
75-
# fields = ('full_name', 'country', 'city', '' 'address', 'phone', 'avatar', )
76-
exclude = ('user', 'role',)
75+
exclude = ('user', 'role', 'avatar',)
7776

7877
def __init__(self, *args, **kwargs):
7978
super(ProfileForm, self).__init__(*args, **kwargs)
8079

8180
for field_name, field in self.fields.items():
8281
self.fields[field_name].widget.attrs['placeholder'] = field.label
8382
self.fields[field_name].widget.attrs['class'] = 'shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500'
84-
self.fields[field_name].widget.attrs['required'] = False
85-
86-
self.fields['avatar'].widget.attrs['class'] = "block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400"
83+
self.fields[field_name].widget.attrs['required'] = False

apps/users/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
), name='password_reset_complete'),
2525

2626
path('profile/', views.profile, name='profile'),
27+
path('upload-avatar/', views.upload_avatar, name='upload_avatar'),
2728
]

apps/users/views.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from apps.users.forms import SigninForm, SignupForm, UserPasswordChangeForm, UserSetPasswordForm, UserPasswordResetForm, ProfileForm
88
from django.contrib.auth import logout
99
from django.urls import reverse
10+
from django.contrib.auth.decorators import login_required
1011

1112
# Create your views here.
1213

@@ -48,10 +49,11 @@ def signout_view(request):
4849
return redirect(reverse('signin'))
4950

5051

52+
@login_required(login_url='/users/signin/')
5153
def profile(request):
5254
profile = get_object_or_404(Profile, user=request.user)
5355
if request.method == 'POST':
54-
form = ProfileForm(request.POST, request.FILES, instance=profile)
56+
form = ProfileForm(request.POST, instance=profile)
5557

5658
if form.is_valid():
5759
form.save()
@@ -61,4 +63,12 @@ def profile(request):
6163
context = {
6264
'form': form,
6365
}
64-
return render(request, 'pages/dashboard/profile.html', context)
66+
return render(request, 'pages/dashboard/profile.html', context)
67+
68+
69+
def upload_avatar(request):
70+
profile = get_object_or_404(Profile, user=request.user)
71+
if request.method == 'POST':
72+
profile.avatar = request.FILES.get('avatar')
73+
profile.save()
74+
return redirect(request.META.get('HTTP_REFERER'))

templates/includes/navigation.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% load static %}
2+
13
<nav class="fixed z-30 w-full bg-white border-b border-gray-200 dark:bg-gray-800 dark:border-gray-700">
24
<div class="px-3 py-3 lg:px-5 lg:pl-3">
35
<div class="flex items-center justify-between">
@@ -335,8 +337,11 @@
335337
class="flex text-sm bg-gray-800 rounded-full focus:ring-4 focus:ring-gray-300 dark:focus:ring-gray-600"
336338
id="user-menu-button-2" aria-expanded="false" data-dropdown-toggle="dropdown-2">
337339
<span class="sr-only">Open user menu</span>
338-
<img class="w-8 h-8 rounded-full" src="https://flowbite.com/docs/images/people/profile-picture-5.jpg"
339-
alt="user photo">
340+
{% if request.user.profile.avatar %}
341+
<img class="w-8 h-8 rounded-full" src="{{ request.user.profile.avatar.url }}" alt="user photo">
342+
{% else %}
343+
<img class="w-8 h-8 rounded-full" src="{% static "dist/img/default-user.png" %}" alt="user photo">
344+
{% endif %}
340345
</button>
341346
</div>
342347

templates/pages/dashboard/profile.html

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,6 @@
44

55
{% block content %}
66

7-
{% comment %} <main>
8-
<div class="px-4 pt-6 flex flex-row gap-7">
9-
<div class="w-full max-w-sm bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
10-
<div class="flex justify-end px-4 pt-4">
11-
<button id="dropdownButton" data-dropdown-toggle="dropdown" class="inline-block text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-1.5" type="button">
12-
<span class="sr-only">Open dropdown</span>
13-
<svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 3">
14-
<path d="M2 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Zm6.041 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM14 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Z"/>
15-
</svg>
16-
</button>
17-
<!-- Dropdown menu -->
18-
<div id="dropdown" class="z-10 hidden text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow w-44 dark:bg-gray-700">
19-
<ul class="py-2" aria-labelledby="dropdownButton">
20-
<li>
21-
<a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white">Edit</a>
22-
</li>
23-
<li>
24-
<a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white">Export Data</a>
25-
</li>
26-
<li>
27-
<a href="#" class="block px-4 py-2 text-sm text-red-600 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white">Delete</a>
28-
</li>
29-
</ul>
30-
</div>
31-
</div>
32-
<div class="flex flex-col items-center pb-10">
33-
{% if request.user.profile.avatar %}
34-
<img class="w-24 h-24 mb-3 rounded-full shadow-lg" src="{{ request.user.profile.avatar.url }}" alt="Bonnie image"/>
35-
{% else %}
36-
<img class="w-24 h-24 mb-3 rounded-full shadow-lg" src="{% static "dist/img/default-user.png" %}" alt="Bonnie image"/>
37-
{% endif %}
38-
<h5 class="mb-1 text-xl font-medium text-gray-900 dark:text-white">
39-
{% if request.user.profile.full_name %}
40-
{{request.user.profile.full_name}}
41-
{% else %}
42-
{{ request.user.username }}
43-
{% endif %}
44-
</h5>
45-
<span class="text-sm text-gray-500 dark:text-gray-400 block mb-2">{{ request.user.profile.role }}</span>
46-
<span class="text-sm text-gray-500 dark:text-gray-400 block mb-2">{{ request.user.profile.phone }}</span>
47-
<span class="text-sm text-gray-500 dark:text-gray-400 block mb-2">{{ request.user.profile.address }}</span>
48-
</div>
49-
</div>
50-
51-
<form method="post" class="w-full border border-gray-200 rounded-lg p-3 shadow dark:bg-gray-800 dark:border-gray-700" enctype="multipart/form-data">
52-
{% csrf_token %}
53-
54-
{% for field in form %}
55-
<div class="relative z-0 w-full mb-6 group">
56-
<label for="{{ field.name }}" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{{ field.label }}</label>
57-
{{ field }}
58-
</div>
59-
{% endfor %}
60-
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
61-
</form>
62-
63-
</div>
64-
</main> {% endcomment %}
65-
667
<div class="grid grid-cols-1 px-4 pt-6 xl:grid-cols-3 xl:gap-4 dark:bg-gray-900">
678
<div class="mb-4 col-span-full xl:mb-2">
689
<nav class="flex mb-5" aria-label="Breadcrumb">
@@ -107,15 +48,19 @@ <h3 class="mb-1 text-xl font-bold text-gray-900 dark:text-white">{{ request.user
10748
<div class="mb-4 text-sm text-gray-500 dark:text-gray-400">
10849
JPG, GIF or PNG. Max size of 800K
10950
</div>
110-
<div class="flex items-center space-x-4">
111-
<button type="button" class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white rounded-lg bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-blue-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">
112-
<svg class="w-4 h-4 mr-2 -ml-1" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M5.5 13a3.5 3.5 0 01-.369-6.98 4 4 0 117.753-1.977A4.5 4.5 0 1113.5 13H11V9.413l1.293 1.293a1 1 0 001.414-1.414l-3-3a1 1 0 00-1.414 0l-3 3a1 1 0 001.414 1.414L9 9.414V13H5.5z"></path><path d="M9 13h2v5a1 1 0 11-2 0v-5z"></path></svg>
113-
Upload picture
114-
</button>
51+
<form method="post" action="{% url "upload_avatar" %}" enctype="multipart/form-data" class="flex items-center space-x-4">
52+
{% csrf_token %}
53+
<label for="avatar">
54+
<div class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white rounded-lg bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-blue-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">
55+
<svg class="w-4 h-4 mr-2 -ml-1" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M5.5 13a3.5 3.5 0 01-.369-6.98 4 4 0 117.753-1.977A4.5 4.5 0 1113.5 13H11V9.413l1.293 1.293a1 1 0 001.414-1.414l-3-3a1 1 0 00-1.414 0l-3 3a1 1 0 001.414 1.414L9 9.414V13H5.5z"></path><path d="M9 13h2v5a1 1 0 11-2 0v-5z"></path></svg>
56+
Upload picture
57+
</div>
58+
</label>
59+
<input onchange="this.form.submit()" type="file" class="hidden" name="avatar" id="avatar">
11560
<button type="button" class="py-2 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700">
11661
Delete
11762
</button>
118-
</div>
63+
</form>
11964
</div>
12065
</div>
12166
</div>
@@ -153,7 +98,7 @@ <h3 class="mb-4 text-xl font-semibold dark:text-white">Language & Time</h3>
15398
<div class="col-span-2">
15499
<div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
155100
<h3 class="mb-4 text-xl font-semibold dark:text-white">General information</h3>
156-
<form method="post" enctype="multipart/form-data">
101+
<form method="post">
157102
{% csrf_token %}
158103

159104
<div class="grid grid-cols-6 gap-6">

0 commit comments

Comments
 (0)