How to create a link between Posts List View and Items List View of the same User

92 views
Skip to first unread message

Ahmed Khairy

unread,
Jul 28, 2020, 11:14:14 PM7/28/20
to Django users

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')

coolguy

unread,
Jul 29, 2020, 7:03:26 PM7/29/20
to Django users
Please share the project directory structure. Send a screen shot or write it. Tnx
Message has been deleted

Jules Andiole

unread,
Jul 29, 2020, 7:36:28 PM7/29/20
to Django users
Replace Item.objects.filter(designer=user).order_by('-timestamp')  with Item.objects.filter(designer = request.user).order_by('-timestamp')
Reply all
Reply to author
Forward
0 new messages