Skip to content

Commit 71f30f8

Browse files
committed
second commit
1 parent 56ce397 commit 71f30f8

File tree

11 files changed

+109
-35
lines changed

11 files changed

+109
-35
lines changed

blog/migrations/0002_alter_post_id.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.0.dev20210706084406 on 2021-07-20 06:39
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('blog', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='post',
15+
name='id',
16+
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
17+
),
18+
]

blog/migrations/0003_post_dislikes.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 4.0.dev20210706084406 on 2021-07-20 07:11
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
11+
('blog', '0002_alter_post_id'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='post',
17+
name='dislikes',
18+
field=models.ManyToManyField(blank=True, related_name='dislikes', to=settings.AUTH_USER_MODEL),
19+
),
20+
]

blog/models.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ class Post(models.Model):
88
date_posted=models.DateTimeField(default=timezone.now)
99
author=models.ForeignKey(User,on_delete=models.CASCADE)
1010
likes=models.ManyToManyField(User,related_name='likes',blank=True)
11+
dislikes = models.ManyToManyField(User, related_name='dislikes', blank=True)
1112
def __str__(self) :
1213
return self.title
1314
def get_absolute_url(self):
1415
return reverse('post-detail',kwargs={'pk':self.pk})
1516
def total_likes(self):
1617
return self.likes.count()
17-
18-
18+
def total_dislikes(self):
19+
return self.dislikes.count()
20+

blog/static/blog/main.css

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ body {
22
background:url("./manga panel.jpg");
33
color: #333333;
44
margin-top: 5rem;
5+
scroll-behavior: smooth;
56
}
67

78
h2, h3, h4, h5, h6 {
@@ -94,4 +95,11 @@ a.article-title:hover {
9495
}
9596
.updown{
9697

97-
}
98+
}
99+
.gif{
100+
position: fixed;
101+
bottom: 0%;
102+
103+
width: 100px;
104+
height: 100px;
105+
}

blog/templates/blog/base.html

+14-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</head>
1919

2020
<body>
21+
2122
<header class="site-header">
2223
<nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
2324
<div class="container">
@@ -46,6 +47,7 @@
4647
</div>
4748
</nav>
4849
</header>
50+
4951
<main role="main" class="container">
5052
<div class="row">
5153
<div class="col-md-8">
@@ -59,7 +61,7 @@
5961
{% block content %}{% endblock %}
6062
</div>
6163
<div class="col-md-4">
62-
<div class="content-section">
64+
<div class="content-section" >
6365
<!-- <h3>Our Sidebar</h3> -->
6466
<!-- <p class='text-muted'>You can put any information here you'd like. -->
6567
<!-- <ul class="list-group"> -->
@@ -74,9 +76,9 @@
7476
{% if user.is_authenticated %}
7577

7678

77-
<div class="media" style="width:max-content height:max-content">
78-
<img class="rounded-circle account-img" src="{{user.profile.image.url}}">
79-
<div class="media-body mr-2">
79+
<div class="media" style="display:inline-block; height:max-content">
80+
<img class="rounded-circle account-img" style="border: 1px solid black;" src="{{user.profile.image.url}}">
81+
<div class="media-body mr-2" style=" display:inline-block;height:max-content">
8082
<h3 >{{user.username}}</h3>
8183
<p class="text-secondary">{{user.email}}</p>
8284
</div>
@@ -85,19 +87,7 @@ <h3 >{{user.username}}</h3>
8587
{% csrf_token %}
8688
<fieldset class="form-group">
8789
<!-- <legend class="border-bottom mb-4">Profile</legend> -->
88-
<h5 class="mb-3">Your Posts</h5>
89-
{% for post in posts %}
90-
91-
92-
<div class="media-body">
93-
94-
<h6><a class="article-title" href="{% url 'post-detail' post.id%}">{{post.title }}</a></h6>
95-
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
96-
97-
98-
</div>
9990

100-
{% endfor %}
10191

10292
</fieldset>
10393

@@ -106,10 +96,17 @@ <h6><a class="article-title" href="{% url 'post-detail' post.id%}">{{post.title
10696
<img src="https://media.giphy.com/media/XgMUfV5i9fTyzVxVE0/giphy.gif" style='width:100%'class="mb-2" >
10797
<h3 class="ml-5 "><a href="{% url 'register'%}" >Register today!</a></h3>
10898
{% endif %}
99+
109100
</div>
110101
</div>
102+
111103
</div>
112104
</main>
105+
<!-- <footer style="background-color:rgb(192, 16, 51);height:100px;opacity:0.5"> -->
106+
<a href="#" id="giphy" >
107+
<img src="https://media.giphy.com/media/1yn2njwLFcUra5z82F/giphy.gif" class="gif">
108+
</a>
109+
</footer>
113110
<!-- Optional JavaScript -->
114111
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
115112
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
@@ -121,7 +118,7 @@ <h3 class="ml-5 "><a href="{% url 'register'%}" >Register today!</a></h3>
121118
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
122119
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
123120
crossorigin="anonymous"></script>
124-
121+
<script src="{% static 'blog/check.js' %}"></script>
125122
</body>
126123

127124
</html>

blog/templates/blog/home.html

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,29 @@
1313
</div>
1414
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
1515
<p class="article-content">{{ post.content }}</p>
16-
<small style="float:inline-end"><a href="{% url 'post-detail' post.id %}">comments</a></small>
16+
17+
18+
<small style="float:inline-end"><a href="{% url 'post-detail' post.id %}">{{post.total_comments}} Comments</a></small>
19+
1720
</div>
1821
<div>
22+
{% if user.is_authenticated %}
1923
<form action="{% url 'like_post' post.pk %}" method="POST">
2024
{% csrf_token %}
2125
<button type="submit" , name="post_id" ,value={{post.id}} , class="btn btn-secondary mb-2 btn-sm"
2226
style='bottom:0'>
23-
<i class="fa fa-angle-up" aria-hidden="true"></i></button>
27+
<i class="fa fa-angle-up" aria-hidden="true"><br>{{post.total_likes}}</i></button>
2428
</form>
2529

26-
<form action="{% url 'like_post' post.pk %}" method="POST">
30+
<form action="{% url 'Dislike_post' post.pk %}" method="POST">
2731
{% csrf_token %}
2832
<button type="submit" , name="post_id" ,value={{post.id}} , class="btn btn-secondary btn-sm "><i
29-
class="fa fa-angle-down" aria-hidden="true"></i></button>
33+
class="fa fa-angle-down" aria-hidden="true"><br>{{post.total_dislikes}}</i></button>
3034
</form>
35+
{% else %}
36+
<button class="btn btn-danger">{{post.total_likes}}<i class="fa fa-angle-up" aria-hidden="true"></i></button>
37+
<button class="btn btn-danger">{{post.total_dislikes}}<i class="fa fa-angle-down" aria-hidden="true"></i></button>
38+
{% endif %}
3139
</div>
3240
</article>
3341
{% endfor %}

blog/templates/blog/post_detail.html

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@
1616
</div>
1717
<h2 class="article-title" >{{object.title }}</h2>
1818
<p class="article-content">{{ object.content }}</p>
19+
{% if user.is_authenticated %}
1920
<ul style="list-style-type: none; display:inline-flex; margin-left:-7%; ">
2021
<li style="margin-right: 5px;"> <form action="{% url 'like_post' object.id %}" method="POST">
2122
{% csrf_token %}
22-
<button type="submit" , name="post_id" ,value="{{object.id}}"" , class="btn btn-secondary mb-2 btn-sm"><i class="fa fa-angle-up" aria-hidden="true"></i></button>
23+
<button type="submit" , name="post_id" ,value="{{object.id}}"" , class="btn btn-secondary mb-2 btn-sm"><i class="fa fa-angle-up" aria-hidden="true">{{object.total_likes}}</i></button>
2324

2425
</form></li>
2526

2627
<li>
27-
<form action="{% url 'like_post' object.id %}" method="POST">
28+
<form action="{% url 'Dislike_post' object.id %}" method="POST">
2829
{% csrf_token %}
29-
<button type="submit" , name="post_id" ,value= "{{object.id}}" , class="btn btn-secondary btn-sm "><i class="fa fa-angle-down" aria-hidden="true"></i></button>
30+
<button type="submit" , name="post_id" ,value= "{{object.id}}" , class="btn btn-secondary btn-sm "><i class="fa fa-angle-down" aria-hidden="true">{{object.total_dislikes}}</i></button>
3031
</form>
3132
</li>
33+
{% else %}
34+
<button class="btn btn-danger">{{post.total_likes}}<i class="fa fa-angle-up" aria-hidden="true"></i></button>
35+
<button class="btn btn-danger">{{post.total_dislikes}}<i class="fa fa-angle-down" aria-hidden="true"></i></button>
3236

37+
{% endif %}
3338
</ul>
3439

35-
<small style="margin-left:95%">comments</small>
40+
<small style="margin-left:95%">{{object.total_comments}} comments</small>
3641
</div>
3742
<br><br>
3843
<a href="{% url 'blog-home' %}" class="btn btn-secondary mb-5">Back</a>

blog/templates/blog/user_post.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{% extends "blog/base.html" %}
22
{% block content %}
3-
<h1 class="mb-3">Posts By {{view.kwargs.username}} ({{page_obj.paginator.count}})</h1>
3+
<div style="background-color: white">
4+
<h1 class="mb-3" style="color:black">Posts By {{view.kwargs.username}} ({{page_obj.paginator.count}})</h1>
5+
</div>
46
{% for post in posts %}
57
<article class="media content-section">
68
<img class="rounded-circle article-img" src="{{post.author.profile.image.url}}">

blog/urls.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
from django.urls import path
3-
from .views import PostListView,PostDetailView,PostCreateView,PostUpdateView,PostDeleteView,UserPostListView,LikeView
3+
from .views import PostListView,PostDetailView,PostCreateView,PostUpdateView,PostDeleteView,UserPostListView,LikeView,DisLikeView
44
from . import views
55
urlpatterns = [
66
path('', PostListView.as_view(), name='blog-home'),
@@ -11,5 +11,7 @@
1111
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
1212
path('about/', views.about, name='blog-about'),
1313
path('like/<int:pk>/',LikeView,name='like_post'),
14+
path('Dislike/<int:pk>/', DisLikeView, name='Dislike_post'),
15+
1416
]
1517

blog/views.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .models import Post
22

3-
from django.shortcuts import render,get_object_or_404
3+
from django.shortcuts import redirect, render,get_object_or_404
44
from django.contrib.auth.mixins import LoginRequiredMixin,UserPassesTestMixin
55

66
from django.contrib.auth.models import User
@@ -76,11 +76,22 @@ def test_func(self):
7676

7777

7878
def LikeView(request, pk):
79-
print(request.POST.get('post_id'))
80-
post = get_object_or_404(Post, id=request.POST.get('post_id'))
81-
post.likes.add(request.user)
82-
return HttpResponseRedirect(reverse('post_detail',args=[str(pk)]))
8379

80+
post = get_object_or_404(Post, pk=pk)
81+
post.likes.add(request.user)
82+
if post.dislikes.filter(id=request.user.id).exists():
83+
post.dislikes.remove(request.user)
84+
85+
return redirect('post-detail',pk=pk)
86+
87+
88+
def DisLikeView(request, pk):
89+
90+
post = get_object_or_404(Post, pk=pk)
91+
post.dislikes.add(request.user)
92+
if post.likes.filter(id=request.user.id).exists():
93+
post.likes.remove(request.user)
94+
return redirect('post-detail', pk=pk)
8495

8596

8697

first/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'django.contrib.messages',
3434
'django.contrib.staticfiles',
3535
'storages',
36+
3637
]
3738

3839
MIDDLEWARE = [

0 commit comments

Comments
 (0)