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

fix user update bug #8

Merged
merged 8 commits into from
Apr 28, 2024
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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
</p>
Django Blogs is a simple and lightweight blogging application built with Django.

It allows users to register and create their own blogs where they can write short and informative posts.
It allows users to register and create blogs where they can write short and informative posts.

## 🔥 Features
- Sign up and create profiles, customize your profiles with avatars
- Create mini-blog posts, update them and share them
- Likes and Comments, like posts and comment your own thoughts.
- Likes and Comments, like posts and comment with your thoughts.

## 📦 Structure
- Django project contains two applications one is `users` and the other one is `blogs`.
- `users` app is controlling all things user related, inculding sign up, registering and updating user information.
- Signals are used to create user profile when a user is created, the user profile contains additional information about a user, such as profile photo.
`users` app controls all things user-related, including signing up, registering and updating user information.
- Signals are used to create a user profile when a user is created, the user profile contains additional information about the user, such as a profile photo.
- `blogs` app is responsible for creating and updating blogs.

## 🏛️ Infrastructure
Expand All @@ -44,9 +44,8 @@ cd djangoblog
python3 -m venv venv
source ./venv/source/activate
```
- Install dependencies
- Install dependencies Setup Postgres database credentials in a .env file, copy the contents of [.sample.env](./sample.env)
```bash
pip install -r requirements-deploy.txt
```
- Setup postgres database credentials in a .env file, copy the contents of [.sample.env](./sample.env)

20 changes: 19 additions & 1 deletion users/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django import forms
from django.forms import ValidationError
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from cloudinary.forms import CloudinaryFileField
from .models import Profile

Expand All @@ -26,7 +28,23 @@ class Meta:

class UpdateUserForm(forms.ModelForm):
email = forms.EmailField()


def clean_username(self):
cleaned_data = super().clean()
username = cleaned_data.get("username")
if User.objects.filter(username=username).exists():
raise ValidationError("Username alreay exists")
return username


def clean_email(self):
cleaned_data = super().clean()
email = cleaned_data.get("email")
if email:
if User.objects.filter(email=email).exists():
raise ValidationError("Email alreay exists")
return email

class Meta:
model = User
fields = [
Expand Down
6 changes: 4 additions & 2 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ def register(request):

def update_user(request):
user_obj = get_object_or_404(User, id=request.user.id)
photo_form = UploadProfilePhotoForm()
if request.method == "POST":
form = UpdateUserForm(request.POST, instance=user_obj)
if form.is_valid():
form.save()
messages.success(request, "Your account has been updated.")
return redirect("profile")


return render(request, "users/profile.html", {"userform": form, "form": photo_form})


def handle_profile_photo(user, profile_photo):
profile = Profile.objects.get(user=user)
profile.image = profile_photo
Expand Down
Loading