I am creating a project where there are Posts and Items, 2 different models in 2 different apps and each has a user who can be the same.
I have created a page for each user to post all related posts called Userpost List view, and I want to add an if statement or a queryset to show a button to link the items related to the same user called Designerpost List view.
I don't know how to proceed as I can fix the NoReverse Error
Here is the models.py
class Post(models.Model): designer = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=100)
Here is the views.py
class UserPostListView(ListView): model = Post template_name = "user_posts.html" context_object_name = 'posts' queryset = Post.objects.filter(admin_approved=True) paginate_by = 6 def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) return Post.objects.filter(designer=user, admin_approved=True).order_by('-date_posted')
Here is the template user_posts.html
{% if item %} <a class="primary btn-lg" href="{% url 'core:designer-posts' item.designer %}" role="button">Go to items</a> {% else %} <a href="{% url 'core:designer-posts' item.designer %}"> <button type="button" class="btn btn-primary btn-lg btn-block">Go to items</button> </a> {% endif %}
here is the item models.py
class Item(models.Model): designer = models.ForeignKey( User, on_delete=models.CASCADE) title = models.CharField(max_length=100)
here is the designerlist views.py that I am trying to link to from the user post view if it is available
class DesignerPostListView(ListView): model = Item template_name = "designer_posts.html" context_object_name = 'items' paginate_by = 6 def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) return Item.objects.filter(designer=user).order_by('-timestamp')
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a761e738-6704-49ac-a610-af4bf74f2dc5o%40googlegroups.com.
No comments:
Post a Comment