问题描述
Views.py - 我想要去一个用户页面,然后点击并按照完全像Twitter的按钮,我知道如何添加用户,我可以看到我的添加变量在我的视图,但我真的没有线索如何实际实现一个按钮,让我跟随用户!我被困在这一整天,这可能是非常明显的,所以任何帮助是非常感谢!我不认为我的模板是需要这个问题,但如果它让我知道!
Views.py - I want to be able to go to a users page, and then click and follow them from a button exactly like twitter, i sort of know how to add users as you can see by my add variable in my view but I really have not clue how to actually implement that into a button that allows me to follow the user! I have been stuck on this for a whole day and it may be very obvious so any help is greatly appreciated! I do not think my template is need for this question but if it is let me know!
@login_required
def home(request, username):
context = {}
if username == request.user.username:
return HttpResponseRedirect('/home /user/{0}'.format(request.user.username))
else:
user = User.objects.get(username=username)
user_profile = UserProfile.objects.filter(user=user)
following = user.userprofile.follows.all()
number = user.userprofile.follows.all().count()
tweet = Tweet.objects.filter(userprofile=user_profile).order_by('date')
yum = Tweet.objects.filter(userprofile=user_profile).count()
add = user.userprofile.follows.add(request.user.userprofile)
context['user'] = user
context['profile'] = user_profile
context['follow'] = following
context['number'] = number
context['tweet'] = tweet
context['yum'] = yum
return render (request, 'homer.html', context)
models.py
models.py
from django.db import models
from django.contrib.auth.models import User
import os
def get_image_path(instance, filename):
return os.path.join('photos', str(instance.user.id), filename)
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio = models.CharField(max_length=120, blank=True, verbose_name='Biography')
follows = models.ManyToManyField('self', related_name='followers', symmetrical=False, blank=True)
theme = models.ImageField(upload_to=get_image_path, blank=True)
profile_picture = models.ImageField(upload_to=get_image_path, blank=True)
def __str__(self):
return self.bio
class Tweet(models.Model):
userprofile = models.ForeignKey(UserProfile)
tweets = models.TextField(max_length=120)
date = models.DateTimeField()
def __str__(self):
return self.tweets
推荐答案
您可以在GET或POST上执行此操作。这是GET的观点,因为这更简单。
You could do this on a GET or a POST. Here's the view it on a GET since that's simpler.
from django.http import JsonResponse
def follow_user(request, user_profile_id):
profile_to_follow = get_object_or_404(UserProfile, pk=user_profile_id)
user_profile = request.user.userprofile
data = {}
if profile_to_follow.follows.filter(id=user_profile.id).exists():
data['message'] = "You are already following this user."
else:
profile_to_follow.follows.add(user_profile)
data['message'] = "You are now following {}".format(profile_to_follow)
return JsonResponse(data, safe=False)
然后在您的urls.py中,您需要将以下内容添加到urlpatterns。
Then in your urls.py you'd need to add the following to your urlpatterns.
url(r'^follow/(?<user_profile_id>[\d]+)/$', views.follow_user)
然后,您需要使用以下JavaScript:
Then you'd need to use some javascript like the following:
$('.follow-button').click(function() {
$.get($(this).data('url'), function(response) {
$('.message-section').text(response.message).show();
});
});
这假设一些html如下:
This assumes some html like the following:
<body>
<div class="message-section" style="display:none;"></div>
{% for user_profile in all_user_profiles %}
<button data-url="{% url "example_app.views.follow_user" user_profile_id=user_profile.id %}"
class="follow-button" type="button">Follow</button>
{% endfor %}
</body>
这篇关于如何在Django中点击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!