Ok in my project, every blogpost has their own group of members. You are only a member if your interest status is 'Accepted', and you get auto added to the member list. Every user can "submit interest" to any blogpost. In case you are confused, the members is a field of BlogPost model, and the status is a field in the InterestInvite model :)
I am trying to make it such that when I press the 'remove member' button, they get removed from the member list, and also the initial interest that they have submitted would be removed. Which means members that are removed can submit interest again.
There are 2 views, the first view is the functionality for the blog post author to be able to remove ANY member from the members list, the second view is the functionality for any of the members to be able to remove THEMSELVES from the members list.
Can any kind soul help to see why my views aren't working
models.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
class BlogPost(models.Model):
title = models.CharField(max_length=50, null=False, blank=False, unique=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField(blank=True, unique=True)
members = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="members")
class Interest(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
blog_post = models.ForeignKey(BlogPost, on_delete=models.CASCADE)
class InterestInvite(models.Model):
ACCEPT = "ACCEPT"
DECLINE = "DECLINE"
PENDING = "PENDING"
STATUS_CHOICES = [
(ACCEPT, "accept"),
(DECLINE, "decline"),
(PENDING, "pending"),
]
interest = models.OneToOneField(Interest, on_delete=models.CASCADE, related_name="interest_invite")
status = models.CharField(max_length=25, choices=STATUS_CHOICES, default=PENDING)
views.py
class MemberListView(UserPassesTestMixin, BlogPostMixin, DetailView):
template_name = "HomeFeed/membersof_yourpost.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
blog_post=self.get_object()
context['blog_post'] = blog_post
return context
def remove_member_view(request, slug):
user = request.user
blog_post = get_object_or_404(BlogPost, slug=slug)
if request.method == "POST":
user_id = request.POST.get("user_id")
if (int(user_id) == user.pk and blog_post.members.filter(pk=user.pk).exists()) or (blog_post.author == request.user and user.pk != int(user_id)):
try:
removee = Account.objects.get(pk=user_id)
blog_post.members.remove(removee)
Interest.objects.filter(user=removee, blog_post=blog_post).delete()
except Account.DoesNotExist:
pass
return redirect('HomeFeed:detail', slug=slug)
urls.py
path("blog/<pk>/<slug>/member-list/", MemberListView.as_view(), name="member_list"),
path("removemember/<slug>/", remove_member_view, name="remove_member"),
members.html
{% for member in object.members.all %}
{% if member == request.user %}
<li class="text-center"><a class="btn btn-info btn-sm mt-3 mb-3" href="{% url 'account:view' member.pk %}">This is you</a></li>
{% else %}
<li class="text-center"><a class="btn btn-primary btn-sm mt-3 mb-3" href="{% url 'account:view' member.pk %}">{{member.username}}</a></li>
{% endif %}
<form method="POST" action="{% url 'HomeFeed:remove_member' blog_post.slug %}">
{% csrf_token %}
<a class="btn btn-primary btn-sm mt-3 mb-3" type="submit" >Remove</a>
</form>
{% endfor %}
question from:
https://stackoverflow.com/questions/65898311/views-py-not-working-and-member-is-not-removed-interest-not-removed-too