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

Solved the functionality of like and comment buttons Issue #22,added like and comment buttons under each post #18,created search user feature #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Binary file modified instagram/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified instagram/__pycache__/settings.cpython-36.pyc
Binary file not shown.
Binary file modified instagram/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified instagram/__pycache__/wsgi.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/apps.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/forms.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified photoapp/__pycache__/views.cpython-36.pyc
Binary file not shown.
8 changes: 7 additions & 1 deletion photoapp/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms
from django.contrib.auth.models import User
from .models import Profile, Post, Comment
from .models import Profile, Post, Comment,cuser
from django.contrib.auth.forms import UserCreationForm

class UserForm(UserCreationForm):
Expand Down Expand Up @@ -29,3 +29,9 @@ class Meta:
model=Profile
fields=('bio','image')


class searchuform(forms.ModelForm):
class Meta:
model=cuser
fields=('username',)

20 changes: 20 additions & 0 deletions photoapp/migrations/0017_auto_20210126_1301.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.0.2 on 2021-01-26 07:31

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('photoapp', '0016_auto_20210125_1053'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='date',
field=models.DateTimeField(default=datetime.datetime(2021, 1, 26, 7, 31, 47, 436426, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions photoapp/migrations/0017_auto_20210130_0138.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.0.2 on 2021-01-29 20:08

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('photoapp', '0016_auto_20210125_1053'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='date',
field=models.DateTimeField(default=datetime.datetime(2021, 1, 29, 20, 8, 31, 868623, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions photoapp/migrations/0018_auto_20210130_0256.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.0.2 on 2021-01-29 21:26

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('photoapp', '0017_auto_20210130_0138'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='date',
field=models.DateTimeField(default=datetime.datetime(2021, 1, 29, 21, 26, 11, 838287, tzinfo=utc)),
),
]
27 changes: 27 additions & 0 deletions photoapp/migrations/0019_auto_20210130_0257.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.0.2 on 2021-01-29 21:27

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('photoapp', '0018_auto_20210130_0256'),
]

operations = [
migrations.CreateModel(
name='cuser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=500)),
],
),
migrations.AlterField(
model_name='comment',
name='date',
field=models.DateTimeField(default=datetime.datetime(2021, 1, 29, 21, 27, 26, 629021, tzinfo=utc)),
),
]
14 changes: 14 additions & 0 deletions photoapp/migrations/0020_merge_20210130_1317.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.0.2 on 2021-01-30 07:47

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('photoapp', '0017_auto_20210126_1301'),
('photoapp', '0019_auto_20210130_0257'),
]

operations = [
]
25 changes: 25 additions & 0 deletions photoapp/migrations/0021_auto_20210130_2344.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.0.2 on 2021-01-30 18:14

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('photoapp', '0020_merge_20210130_1317'),
]

operations = [
migrations.AddField(
model_name='post',
name='nolike',
field=models.CharField(default='0', max_length=200),
),
migrations.AlterField(
model_name='comment',
name='date',
field=models.DateTimeField(default=datetime.datetime(2021, 1, 30, 18, 14, 58, 51365, tzinfo=utc)),
),
]
Binary file modified photoapp/migrations/__pycache__/0001_initial.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file modified photoapp/migrations/__pycache__/0003_post_image.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified photoapp/migrations/__pycache__/0008_comment.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified photoapp/migrations/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions photoapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Post(models.Model):
location=models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author=models.ForeignKey(User,on_delete=models.CASCADE)
nolike=models.CharField(default='0',max_length=200)


def __str__(self):
Expand All @@ -54,4 +55,6 @@ class Likes(models.Model):
def __str__(self):
return str(self.post)

class cuser(models.Model):
username=models.CharField(max_length=500)

2 changes: 2 additions & 0 deletions photoapp/templates/photoapp/invalidsearch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<H1>NO SUCH USER FOUND!<H1>

6 changes: 4 additions & 2 deletions photoapp/templates/photoapp/postlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ <h1>THE

{% for i in posts %}
<br>

<h1>{{i.author}}</h1><img class="img-thumbnail" src="{{ i.author.profile.image.url }}" style="width:100px;height:100px;"><br><br>
<img class="img-fluid img-thumbnail" src="{{ i.image.url }}" style="width:300px;height:500px;">
<img class="img-fluid img-thumbnail" src="{{ i.image.url }}" style="width:300px;height:500px;"><br>
Likes:{{i.nolike}}
<a href="{% url 'like-post' i.id %}" ><button type="submit" value="{{i.id}}" class="btn btn-primary">Like</button></a>{{i.Likes.count}}<br>
<a button class="btn btn-outline-danger" href="{% url 'create-comment' i.id %}">add comment</button></a><br>
<a href="{% url 'post-detail' i.id %}"><h3>{{i.caption}}</h3></a><br>

{{i.date_posted}}<br>
Expand Down
6 changes: 6 additions & 0 deletions photoapp/templates/photoapp/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<form action="{% url 'searchuser' %}" method="POST" enctype="multipart/form-data">
<label>ENTER USERNAME:&nbsp;</label>
{{form.username}}
{% csrf_token %}
<br><input type="submit" value="search">
</form>
5 changes: 5 additions & 0 deletions photoapp/templates/photoapp/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<H1>USER:</H1>

<br>USERNAME:{{k.username}}
<br>NAME:{{k.first_name|title}} {{k.last_name|title}}
<br>Email:{{k.email}}
4 changes: 2 additions & 2 deletions photoapp/templates/photoapp/viewpost.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ <h2>User is -{{posts.author}}</h2><br>
<h4>{{posts.caption}}</h4><br>
<img class="img-fluid img-thumbnail" src="{{ posts.image.url }}" style="width:300px;height:500px;"><br>likes is -{{l}}<br>



<a href="{% url 'like-post' posts.id %}" ><button type="submit" value="{{posts.id}}" class="btn btn-primary">Like</button></a><br>
<a button class="btn btn-outline-danger" href="{% url 'create-comment' posts.id %}">add comment</button></a><br>

{% for i in co %}
Expand Down
1 change: 1 addition & 0 deletions photoapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
path('home/<key>/create_comment',views.create_comment,name='create-comment'),

path('home/<key>/like_post', views.like_post, name='like-post'),
path('home/search',views.searchuser,name='searchuser'),
]
74 changes: 67 additions & 7 deletions photoapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth import authenticate, login, logout

from django.contrib.auth.decorators import login_required
from .forms import UserForm,CreatePostForm,UpdatePostForm,CreateCommentForm, UpdateProfileForm
from .forms import UserForm,CreatePostForm,UpdatePostForm,CreateCommentForm, UpdateProfileForm,searchuform
from django.contrib.auth.models import User
from .models import Profile,Post, Comment, Likes
from django.core.files.storage import FileSystemStorage
Expand Down Expand Up @@ -89,7 +89,7 @@ def home(request):
print(users)

pk=request.user

#m=[]
following=[]
posts=Post.objects.order_by("-date_posted")
for i in users:
Expand All @@ -103,8 +103,17 @@ def home(request):

following.append(i.pk)
#print(following)
for n in posts:
k=Likes.objects.filter(post=n)
tno=k.count()
n.nolike=tno
#print(n.nolike)
#print(n.id)
#m.insert(n.id,tno)

post_list=posts
#print(post_list)
#print(m)
print(post_list)
return render(request,'photoapp/postlist.html',{'posts':post_list})

def confirm_delete(request,key):
Expand Down Expand Up @@ -177,6 +186,7 @@ def create_comment(request,key):
next = request.POST.get('next',None)
#return HttpResponseRedirect(next)
#return redirect('/home/key/')

return redirect('/home/%s' % key )
else:
form=CreateCommentForm()
Expand All @@ -186,15 +196,65 @@ def create_comment(request,key):
return render(request,'photoapp/create_comment.html',{'form':form})
def like_post(request,key):
posts=Post.objects.get(id=key)
t=1
liked=Likes()
liked.post=posts
liked.users=request.user
liked.save()
return redirect('/home/%s' % key )

liky=Likes.objects.filter(post=posts)
print('3')
for lik in liky:
if (lik.users==request.user):
t=0
break
if t:
liked.users=request.user
liked.save()
else:
lik.delete()



return redirect('/home')



def searchuser(request):
if request.method=='POST':
#print('2')
t=0
form=searchuform(request.POST)
usn=User.objects.all()
#print(len(usn))
#f=form.cleaned_data['username']
#print(form)
#for i in usn:
#if i.username==form.username:
#print(i.username)


if form.is_valid():
#print('17')
#print(form)
f=form.cleaned_data['username']
#print(f)
for k in usn:
if k.username==f:
#print(k.last_name)
t=1
break
if t:
return render(request,'photoapp\show.html',{'k':k})
else:
return render(request,'photoapp\invalidsearch.html')
# print('1')
# j=form.save(commit=False)
# usinst=User.objects.get(username=j.username)
# print(usinst)
# return render(request,'show.html',{'usinst':usinst})
#else:
# form=searchuform()
else:
form=searchuform()
return render(request,'photoapp\search.html',{'form':form})



Expand Down