Model.py
class Contact(models.Model):
user_from = models.ForeignKey(User,related_name = 'user_from_set')
user_to = models.ForeignKey(User,related_name = 'user_to_set')
created = models.DateTimeField(auto_now_add = True, db_index = True)
class Meta:
ordering = ('-created',)
def __str__(self):
return '{} user_follows {}'.format(self.user_from, self.user_to)
User.add_to_class('user_following', models.ManyToManyField('self', through = Contact, related_name = 'user_followers', symmetrical = False))
-------------------
views.py
from django.contrib.auth.models import User
def ProfileView(request,username):
user = get_object_or_404(User, username=username, is_active=True)
users=User.objects.get(username=username)
id = User.objects.get(username = user).id
add = Profile.objects.get(user_id = id).address
imag = Profile.objects.get(user_id = id).image
d1 = imag.url
friend = Friend.objects.friends(user)
return render(request, 'profile_view.html',{'users':users,'add':add,'imag':d1,'user':user})
return render(request,'tweet_view.html',{'form':form})
#following
from django.views.decorators.http import require_POST
from common.decorators import ajax_required
from django.http import JsonResponse
from .models import Contact
@ajax_required
@require_POST
@login_required
def user_follow(request):
user_id = request.POST.get('id')
action = request.POST.get('action')
if user_id and action:
try:
user = User.objects.get(id=user_id)
if action == 'follow':
Contact.objects.get_or_create(user_from=request.user,
user_to=user)
create_action(request.user, 'is following', user)
else:
Contact.objects.filter(user_from=request.user,
user_to=user).delete()
return JsonResponse({'status':'ok'})
except User.DoesNotExist:
return JsonResponse({'status':'ko'})
return JsonResponse({'status':'ko'})
--------
urls.py
url(r'^search/$',views.search,name = 'search'),
url(r'^user/follow/$', views.user_follow, name='user_follow'),
#Add User_Profile
url(r'^user/(?P<username>.\D+)/$', views.ProfileView),
---------
profile_view.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
{%if user%}
{%if users%}
{%if imag%}
{%if add%}
<p>{{users}}</p>
<p>{{add}}</p>
<img src="{{imag}}" alt="">
<br><br><br>
{% with total_followers=user.followers.count %}
<span class="count">
<span class="total">{{ total_followers }}</span>
follower{{ total_followers|pluralize }}
</span>
<a href="#" data-id="{{
user.id }}" data-action="{% if request.user in user.followers.all %}un{% endif %}follow" class="follow button">
{% if request.user not in user.followers.all %}
Follow
{% else %}
Unfollow
{% endif %}
</a>
{% endwith %}
{%endif%}
{%endif%}
{%endif%}
{%endif%}
<script>
$(document).ready(function(){
$('a.follow').click(function(e){
e.preventDefault();
$.post('{% url "user_follow" %}',
{
id: $(this).data('id'),
action: $(this).data('action')
},
function(data){
if (data['status'] == 'ok')
{
var previous_action = $('a.follow').data('action');
// toggle data-action
$('a.follow').data('action', previous_action == 'follow' ? 'unfollow' : 'follow');
// toggle link text
$('a.follow').text(previous_action == 'follow' ? 'Unfollow' : 'Follow');
// update total followers
var previous_followers = parseInt($('span.count.total').text());
$('span.count.total').text(previous_action == 'follow' ? previous_followers + 1 : previous_followers - 1);
}
});
});
});
</script>
</body>
</html>
