- Introduction
- Team Members
- Technologies Used
- Project Structure
- Database Models
- Use Cases
- API Endpoints
- Setup Instructions
This project is a social media application built using Django. It allows users to register, create posts, comment on posts, and like or dislike posts. The frontend is built using HTML, CSS, and JavaScript, while the backend is powered by Django and Django REST framework.
- Mohamed Osama
- Ahmed Samir
- Mohamed Mahmoud
- Backend: Django, Django REST framework
- Frontend: HTML, CSS, JavaScript
- Database: SQLite
- Other: jQuery
db.sqlite3
front end/
about.html
contact.html
dashboard.html
feed.html
index.html
login.html
post.html
register.html
usrinfo.html
global/
models.py
urls.py
views.py
media/
posts/
profile_images/
posts/
serializers.py
views.py
social_media_app/
settings.py
urls.py
wsgi.py
userapp/
models.py
serializers.py
views.py
Defined in userapp/migrations/0001_initial.py
:
class CustomUser(models.Model):
id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
password = models.CharField(max_length=128, verbose_name='password')
last_login = models.DateTimeField(blank=True, null=True, verbose_name='last login')
is_superuser = models.BooleanField(default=False, verbose_name='superuser status')
username = models.CharField(max_length=150, unique=True, verbose_name='username')
is_staff = models.BooleanField(default=False, verbose_name='staff status')
is_active = models.BooleanField(default=True, verbose_name='active')
date_joined = models.DateTimeField(default=timezone.now, verbose_name='date joined')
email = models.EmailField(max_length=254, unique=True)
bio = models.TextField(blank=True)
image = models.ImageField(blank=True, null=True, upload_to='profile_images/')
birth_date = models.DateField(default=datetime.date.today)
registration_date = models.DateField(auto_now_add=True)
first_name = models.CharField(blank=True, max_length=30)
last_name = models.CharField(blank=True, max_length=150)
groups = models.ManyToManyField(blank=True, to='auth.group', verbose_name='groups')
user_permissions = models.ManyToManyField(blank=True, to='auth.permission', verbose_name='user permissions')
Defined in posts/models.py
:
class Post(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='posts')
content = models.TextField()
image = models.ImageField(upload_to='posts/', blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
likes = models.ManyToManyField(CustomUser, related_name='liked_posts', blank=True)
dislikes = models.ManyToManyField(CustomUser, related_name='disliked_posts', blank=True)
class Meta:
ordering = ['-created_at']
Defined in posts/models.py
:
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='comments')
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
- User Registration: Users can register with a unique username and email.
- User Login: Registered users can log in to the application.
- Create Post: Logged-in users can create new posts with text and optional images.
- Comment on Post: Users can comment on posts.
- Like/Dislike Post: Users can like or dislike posts.
- View Feed: Users can view a feed of all posts.
- User Profile: Users can view their profile and other users' profiles.
Defined in posts/views.py
:
@login_required
@require_POST
def add_comment(request, post_id):
post = get_object_or_404(Post, id=post_id)
comment_content = request.POST.get('content')
if comment_content:
comment = Comment.objects.create(user=request.user, post=post, content=comment_content)
return JsonResponse({
'success': True,
'user_id': comment.user.id,
'username': comment.user.username,
'content': comment.content,
'image': comment.user.image.url if comment.user.image else None,
'count_comment': post.comments.count()
})
return JsonResponse({'success': False}, status=400)
Defined in posts/views.py
:
@api_view(['POST'])
def like_post(request, post_id):
post = Post.objects.get(id=post_id)
user = request.user
if user in post.likes.all():
post.likes.remove(user)
liked = False
else:
post.likes.add(user)
post.dislikes.remove(user)
liked = True
return Response({
'success': True,
'count': post.likes.count(),
'disliked_count': post.dislikes.count(),
'liked': liked
}, status=status.HTTP_200_OK)
Defined in posts/views.py
:
@api_view(['POST'])
def dislike_post(request, post_id):
post = Post.objects.get(id=post_id)
user = request.user
if user in post.dislikes.all():
post.dislikes.remove(user)
disliked = False
else:
post.dislikes.add(user)
post.likes.remove(user)
disliked = True
return Response({
'success': True,
'count': post.dislikes.count(),
'liked_count': post.likes.count(),
'disliked': disliked
}, status=status.HTTP_200_OK)
-
Clone the repository:
git clone https://github.com/ahmedsamir45/social_media_app.git cd <repository-directory>
-
Install dependencies:
pip install -r requirements.txt
-
Apply migrations:
python manage.py migrate
-
Run the development server:
python manage.py runserver
-
Access the application: Open your browser and navigate to
http://127.0.0.1:8000/
.