Django exclude friend requests
I am making a social media app and I want to make a friend system. Here is the code:
Models.py
class Requests(models.Model):
name = models.CharField(max_length=200)
friend = models.CharField(max_length=200)
answer = models.CharField(max_length=200,blank=True)
def __str__(self):
return self.name
Html
<div class='form'>
<form action='' method='post'>
{%csrf_token%}
<input type='text' class='form-control' name='name'>
<button name='first' style='margin-top:20px;' class='btn btn-primary btn-lg'>Search</button>
</form>
</div>
{%if messages%}
{%for msg in messages%}
<div class='user' style='margin-top:50px;margin-left:310px;'>
<form action='' method='post'>
{%csrf_token%}
<img style='width:70px;height:70px;border-radius:15px;' src='/media/{{msg.extra_tags}}'></img>
<p style='color:white;margin-left:5px;font-size:18px;display:inline-block;'>{{msg}}</p>
<input type='hidden' name='name2' value='{{msg}}'>
<button name='second' style='margin-left:10px;' class='btn btn-primary' onclick='myfuc()'>Send</button>
</form>
</div>
{%endfor%}
{%endif%}
Views.py
def friends(request):
if request.method == 'POST':
if 'first' in request.POST:
name = request.POST['name']
users = User.objects.filter(username__startswith=name).all()
if not users:
pass
else:
for i in users:
messages.info(request,i.username,extra_tags=i.profile.profpic)
if 'second' in request.POST:
name2 = request.POST['name2']
request5 = Requests.objects.filter(name=request.user.username,friend=name2).first()
if request5:
pass
else:
requests3 = Requests(name=request.user.username,friend=name2)
requests3.save()
return render(request,'requested.html')
I want every time a user makes a friend request then the user gets excluded from the messages.info(request,i.username,extra_tags=i.profile.profpic)
and doesn't appear when a user searches again.Please help me.
Thanks.
There are probably many problems inside your template and even yor models. I try to give you a possible solution: First of all, you can't make multiple POST form in your template or you have to specify the url in your template. Besides, if you want to simply search something, I suggest you to use a GET form. If you want to exclude from your search some user that have already searched, you should also add a boolean field to your user model (I don't know if your are using your own custom user model or the built in one). So, I write you what would be my approach. models.py
class Requests(models.Model):
name = models.CharField(max_length=200)
friend = models.CharField(max_length=200)
answer = models.CharField(max_length=200,blank=True)
def __str__(self):
return self.name
class User(models.Model or inherith from AbstractUser or whatever):
#whatever fields you have
friend_request = models.BooleanField(default=False)
html:
<div class='form'>
<form action='' method='get'> <!-- Here you should use a get request if you want make a search -->
<input type='text' class='form-control' name='name'>
<button name='first' style='margin-top:20px;' class='btn btn-primary btn-lg'>Search</button>
</form>
</div>
{%if messages%}
{%for msg in messages%}
<div class='user' style='margin-top:50px;margin-left:310px;'>
<form action='' method='post'>
{%csrf_token%}
<img style='width:70px;height:70px;border-radius:15px;' src='/media/{{msg.extra_tags}}'></img>
<p style='color:white;margin-left:5px;font-size:18px;display:inline-block;'>{{msg}}</p>
<input type='hidden' name='name2' value='{{msg}}'>
<button name='second' style='margin-left:10px;' class='btn btn-primary' onclick='myfuc()'>Send</button>
</form>
</div>
{%endfor%}
{%endif%}
Now, instead of having one view for handling many stuff, I'd split in 2 different views: 1 for handling get or post request and the other one for rendering the template. However, you can just handle in one unique view with some code like that: views.py
def friends(request):
if request.method == 'GET':
name = request.POST['name']
users = User.objects.filter(username__startswith=name,friend_request=False) #this way you can filter just the users you didn't request friendship
if not users:
pass
else:
for i in users:
messages.info(request,i.username,extra_tags=i.profile.profpic)
if request.method == 'POST':
name2 = request.POST['name2']
request5 = Requests.objects.filter(name=request.user.username,friend=name2).first()
if request5:
pass
else:
requests3 = Requests(name=request.user.username,friend=name2)
requests3.save()
return render(request,'requested.html')
I don't know exactly what you wanna achieve in the second part of the view so I left almost as it is. Let mw know if that worked for you