Commit d291048e authored by James Esguerra's avatar James Esguerra

Added UserForm to profile and handled POST request to change profile photo

parent b4a59bb1
...@@ -6,6 +6,14 @@ class HomeForm(forms.Form): ...@@ -6,6 +6,14 @@ class HomeForm(forms.Form):
name = forms.CharField(label='', max_length=100) name = forms.CharField(label='', max_length=100)
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['profile_photo']
label = {
'profile_photo': '',
}
class KeyForm(forms.ModelForm): class KeyForm(forms.ModelForm):
class Meta: class Meta:
model = Key model = Key
......
...@@ -2,10 +2,8 @@ from django.db import models ...@@ -2,10 +2,8 @@ from django.db import models
class User(models.Model): class User(models.Model):
nickname = models.CharField(max_length=50, default='Your nickname') nickname = models.CharField(max_length=50)
bio = models.TextField( bio = models.TextField(max_length=100)
max_length=100,
default='A short description about yourself.')
profile_photo = models.ImageField() profile_photo = models.ImageField()
def __str__(self): def __str__(self):
......
...@@ -3,6 +3,7 @@ from django.shortcuts import render, redirect ...@@ -3,6 +3,7 @@ from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from .forms import ( from .forms import (
HomeForm, HomeForm,
UserForm,
KeyForm, KeyForm,
ItemsThisWeekForm, ItemsThisWeekForm,
ItemsTodayForm, ItemsTodayForm,
...@@ -23,17 +24,28 @@ def ShowHome(request): ...@@ -23,17 +24,28 @@ def ShowHome(request):
def ShowProfile(request): def ShowProfile(request):
form = UserForm()
if User.objects.count() == 0: if User.objects.count() == 0:
user = User( user = User(
nickname='Your nickname', nickname='Your nickname',
bio='A short description about yourself.' bio='A short description about yourself.',
profile_photo='default.jpg'
) )
user.save() user.save()
else: else:
user = User.objects.get(pk=1) user = User.objects.get(pk=1)
context = { context = {
'form': form,
'user': user, 'user': user,
} }
if request.method == 'POST':
current = User.objects.get(pk=1) #allows us to edit current photo
form = UserForm(request.POST, request.FILES, instance=current)
if form.is_valid():
form.save()
return redirect('profile')
return render(request, 'profile.html', context) return render(request, 'profile.html', context)
......
...@@ -48,3 +48,9 @@ a { ...@@ -48,3 +48,9 @@ a {
font-size: 1.5em; font-size: 1.5em;
padding: 150px; padding: 150px;
} }
#image {
max-width: 200px;
max-height: 200px;
object-fit: contain;
}
...@@ -14,7 +14,13 @@ ...@@ -14,7 +14,13 @@
<p><b>Bio:</b> {{ user.bio }}</p> <p><b>Bio:</b> {{ user.bio }}</p>
</div> </div>
<div> <div id='image-container'>
<img class='image' src='{{user.profile_photo.url}}'>
<form action='' method='POST' enctype='multipart/form-data'>
{% csrf_token %}
{{ form.profile_photo }}
<input type='submit' name='Submit' value='Change'>
</form>
</div> </div>
</div> </div>
......
...@@ -125,3 +125,6 @@ USE_TZ = True ...@@ -125,3 +125,6 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
from django.contrib import admin from django.contrib import admin
from django.urls import include, path from django.urls import include, path
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [ urlpatterns = [
path('', include('pages.urls')), path('', include('pages.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment