Reputation: 13
Hey guys I am writing a following system for my site and when I go into the admin page and force the user to follow it works fine but my following button doesnt work, it just refreshes the site and just wondering if anyone could help me with it please. Thanks in advance.
EDITED
this is my html
`<button type="button" class="btn btn-primary btn-lg" href="{% url "accounts:follow" username=object.username%}">
{% if following %}Unfollow{% else %}Follow{% endif %}
</button>`
urls.py
urlpatterns = [
url(r'^profile/(?P<username>[\w.@+-]+)', views.UserDetailView.as_view(), name="viewprofile"),
url(r'^profile/(?P<username>[\w.@+-]+)/follow', views.UserFollowView.as_view(), name="follow"),]
views.py
class UserFollowView(View):
def get(self, request, username, *args, **kwargs):
toggle_user = get_object_or_404(User, username__iexact=username)
if request.user.is_authenticated():
is_following = UserProfile.objects.toggle_follow(request.user, toggle_user)
return redirect("accounts:viewprofile")
models.py
class UserProfileManager(models.Manager):
use_for_related_fields = True
def all(self):
qs = self.get_queryset().all()
try:
if self.instance:
qs = qs.exclude(user=self.instance)
except:
pass
return qs
def toggle_follow(self, user, to_toggle_user):
user_profile, created = UserProfile.objects.get_or_create(user=user)
if to_toggle_user in user_profile.following.all():
user_profile.following.remove(to_toggle_user)
added = False
else:
user_profile.following.add(to_toggle_user)
added = True
return added
def is_following(self, user, followed_by_user):
user_profile, created = UserProfile.objects.get_or_create(user=user)
if created:
return False
if followed_by_user in user_profile.following.all():
return True
return False
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile')
following = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True,related_name='followed_by')
objects = UserProfileManager()
def __str__(self):
return str(self.following.all().count())
def get_following(self):
users = self.following.all()
return users.exclude(username=self.user.username)
Upvotes: 0
Views: 58
Reputation: 58
Try adding a '$' to the end of your urls. Django tries to match the urls from first to last, and since your first regular expression matches the url you're trying to visit, it doesn't bother looking further and goes to it. The '$' will end the regular expression.
urls.py
urlpatterns = [
url(r'^profile/(?P<username>[\w.@+-]+)$', views.UserDetailView.as_view(), name="viewprofile"),
url(r'^profile/(?P<username>[\w.@+-]+)/follow$', views.UserFollowView.as_view(), name="follow"),]
Upvotes: 1
Reputation: 8966
If the page "just refreshes", it sounds like the button HTML itself is incorrect. I had this problem yesterday and after an hour of digging into my JS and Python, I realized that a button automatically submits a form on the page unless it is declared with the type "button" like this:
<button type="button" ....>
Upvotes: 0