Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added profile update feature-redirect not letting pass context #3

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified media/default.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions taskstodo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
remove images and files on deletion or updation
redirect not letting pass context
21 changes: 16 additions & 5 deletions users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@
from django.contrib.auth.forms import UserCreationForm


class UserForm(UserCreationForm):
class UserRegisterForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'first_name','last_name',
'email', 'password1', 'password2']
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']


class ProfileForm(forms.ModelForm):
class ProfileRegisterForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image', 'rollno', 'year', 'branch', 'techskills','cv']
fields = ['image', 'rollno', 'year', 'branch', 'techskills', 'cv']


class UserUpdateForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email']


class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image', 'rollno', 'year', 'branch', 'techskills', 'cv']
10 changes: 9 additions & 1 deletion users/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.contrib.auth.models import User

from PIL import Image

YEAR_CHOICES = (
('1', '1st'),
Expand All @@ -22,3 +22,11 @@ class Profile(models.Model):

def __str__(self):
return f"{self.user}({self.rollno})"

def save(self, *args, **kwargs):
super(Profile, self).save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
dimensions = (300, 300)
img.thumbnail(dimensions)
img.save(self.image.path)
22 changes: 21 additions & 1 deletion users/templates/users/profile.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{% extends 'home/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
Hi {{ user.username }} <br> You are on profile page.
<img src="{{ user.profile.image.url }}" alt="">
<br>
<img src="{{ user.profile.image.url }}" alt="" style="height:100px;height:100px">
<div>
{% if messages %}
{% for message in messages %}
<div>{{message}}</div>
{% endfor %}
{% endif %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend>Profile Info</legend>
{{ user_form|crispy }}
{{ profile_form|crispy }}
</fieldset>
<div>
<button type="submit">Update<button>
</div>
</form>
</div>
{% endblock content %}
2 changes: 1 addition & 1 deletion users/templates/users/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div>{{message}}</div>
{% endfor %}
{% endif %}
<form method="POST" enctype="multipartform-data">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend>Register</legend>
Expand Down
50 changes: 34 additions & 16 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
from django.contrib.auth.models import User
from django.urls import reverse
from django.shortcuts import redirect, render
from .forms import UserForm, ProfileForm
from .forms import UserRegisterForm, ProfileRegisterForm, UserCreationForm, ProfileUpdateForm, UserUpdateForm
from django.contrib import messages
from .models import Profile
from django.contrib.auth.decorators import login_required

@login_required
def profile(request):
context = {
'title':'Profile'
}
return render(request, 'users/profile.html', context)

def register(request):
# request.POST is actually a dictionary containing all the form fields as key and there form values as values for respective keys
# request.user -> person creating request i.e. person logged in django-admin
if request.method == 'POST':
user_form = UserForm(request.POST)
profile_form = ProfileForm(request.POST, request.FILES)
user_form = UserRegisterForm(request.POST)
profile_form = ProfileRegisterForm(request.POST, request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
newusername = user_form.cleaned_data.get('username')
newuser = User.objects.filter(username = newusername).first()

profile_form = ProfileForm(request.POST,request.FILES, instance = newuser.profile)
profile_form = ProfileRegisterForm(request.POST,request.FILES, instance = newuser.profile)
# instance tells to which profile should we save the data
# we could manually also save the data
# newuserprofile = Profile(user = newuser, rollno = request.Post.rollno ............)
# newuserprofile.save()
profile_form.save()

messages.success(request, f"Account created for {newusername}!")
context = {
'title':'Login'
}
return redirect('login', context)
return redirect('login')
else:
messages.error(request, 'Please correct the error below.')
else:
user_form = UserForm()
profile_form = ProfileForm()
user_form = UserRegisterForm()
profile_form = ProfileRegisterForm()

context = {
'title': 'Register',
Expand All @@ -49,6 +41,32 @@ def register(request):

return render(request, 'users/register.html', context)


@login_required
def profile(request):
if request.method == 'POST':
user_update_form = UserUpdateForm(request.POST, instance = request.user)
profile_update_form = ProfileUpdateForm(request.POST, request.FILES, instance = request.user.profile)
if user_update_form.is_valid() and profile_update_form.is_valid():
user_update_form.save()
profile_update_form.save()

messages.success(request, "Your profile has been updated!")
return redirect('profile')
else:
messages.error(request, 'Please correct the error below.')
else:
user_update_form = UserUpdateForm(instance = request.user)
profile_update_form = ProfileUpdateForm(instance = request.user.profile)

context = {
'title': 'Profile',
'user_form': user_update_form,
'profile_form': profile_update_form
}

return render(request, 'users/profile.html', context)

# Database queries
# Profile.objects.all() -> it gives queryset
# Profile.objects.all().first() -> first element of queryset
Expand Down