diff --git a/db.sqlite3 b/db.sqlite3 index 5f123db..919b458 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/media/default.jpg b/media/default.jpg index 330146e..8e55dee 100644 Binary files a/media/default.jpg and b/media/default.jpg differ diff --git a/taskstodo b/taskstodo new file mode 100644 index 0000000..4fea0d5 --- /dev/null +++ b/taskstodo @@ -0,0 +1,2 @@ +remove images and files on deletion or updation +redirect not letting pass context \ No newline at end of file diff --git a/users/forms.py b/users/forms.py index b43f55d..4a58857 100644 --- a/users/forms.py +++ b/users/forms.py @@ -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'] diff --git a/users/models.py b/users/models.py index a189366..5887a16 100644 --- a/users/models.py +++ b/users/models.py @@ -1,6 +1,6 @@ from django.db import models from django.contrib.auth.models import User - +from PIL import Image YEAR_CHOICES = ( ('1', '1st'), @@ -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) diff --git a/users/templates/users/profile.html b/users/templates/users/profile.html index 0d3292c..36dd5b7 100644 --- a/users/templates/users/profile.html +++ b/users/templates/users/profile.html @@ -1,5 +1,25 @@ {% extends 'home/base.html' %} +{% load crispy_forms_tags %} {% block content %} Hi {{ user.username }}
You are on profile page. - +
+ +
+ {% if messages %} + {% for message in messages %} +
{{message}}
+ {% endfor %} + {% endif %} +
+ {% csrf_token %} +
+ Profile Info + {{ user_form|crispy }} + {{ profile_form|crispy }} +
+
+
+
+
{% endblock content %} diff --git a/users/templates/users/register.html b/users/templates/users/register.html index 900bcfc..8b9db5c 100644 --- a/users/templates/users/register.html +++ b/users/templates/users/register.html @@ -7,7 +7,7 @@
{{message}}
{% endfor %} {% endif %} -
+ {% csrf_token %}
Register diff --git a/users/views.py b/users/views.py index a00695b..0587b08 100644 --- a/users/views.py +++ b/users/views.py @@ -1,29 +1,24 @@ 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 ............) @@ -31,15 +26,12 @@ def register(request): 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', @@ -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