Skip to content

ajenez/Network-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Network-Project

Basic network site developed with Python, HTML, CSS, and Javascript using Django Framework

This project is a network site where users have the ability to register, login, view all current posts made by themselves or other users, create and edit a post, like and unlike a post, view individual profiles, follow and unfollow users, and access a "following" page that only displays posts made by the users they follow.

Personal Favorite Code Block - This block tested my understanding of javascript and required learning to route fetch requests through a specific Django view to make changes within the database to add and remove likes using POST/DELETE requests and edit current posts using a PUT request.

https://github.com/ajenez/Network-Project/blob/962a53c68dfe139bd7b77457b4a96203286a1390/network/static/network/index.js

Extra: This was my first time routing through a view that would not actually be "viewed". The purpose of this view is collecting JSON data, converting it into a format the database could read, and then changing the database accordingly.

def posting(request):
if request.method == "POST":
data = json.loads(request.body)
post = data.get("post_id", "")
post_id = Post.objects.get(id=post)
liker = data.get("liker_id", "")
liker_id = User.objects.get(username=liker)
like = Like.objects.create(post_id=post_id, liker_id=liker_id)
like.save()
return JsonResponse({"message": "Like added"})
if request.method == "DELETE":
data = json.loads(request.body)
post = data.get("post_id", "")
post_id = Post.objects.get(id=post)
liker = data.get("liker_id", "")
liker_id = User.objects.get(username=liker)
like = Like.objects.get(post_id=post_id, liker_id=liker_id)
like.delete()
return JsonResponse({"message": "Like deleted"})
if request.method == "PUT":
if not request.user.is_authenticated:
return HttpResponseRedirect(reverse('login'))
data = json.loads(request.body)
print(data)
post_id = data.get("post_id", "")
print(post_id)
post = Post.objects.get(id=post_id)
print(post)
content = data.get("content", "")
if content:
if request.user != post.user_id:
return JsonResponse({"error": "Can only edit your own posts"})
post.post_content = content
post.save()
#like.save()
return JsonResponse({"message": "Post edited successfully"})
if request.method != "POST" and request.method != "PUT" and request.method != "DELETE":
return JsonResponse({"error": "PUT, POST or DELETE request required."}, status=400)

Video demonstration: https://www.youtube.com/watch?v=MSJV6zNvL2Q

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published