Linking 2 List views for different models who share the same user

Submitted 3 years, 8 months ago
Ticket #59
Views 261
Language/Framework Python
Priority Low
Status Closed

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 User post 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 Designer post 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 designer list 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')

Submitted on Aug 04, 20
add a comment

1 Answer

@Ahmed.Hisham87 Thanks for your detailed information. As you are using Class Based View, i think it should be easy. You can use get_context-data method to use another model as well.

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

    def get_context_data(self,request, **kwargs):
        context = super().get_context_data(**kwargs)
        item_qs = Item.objects.filter(user=self.request.user)
        context['item'] = item_qs
        return context

Then in your template use this item as if else condition to handle it.

{% if item %}
   <a class="primary btn-lg" href="{% url 'core:designer-posts' item.designer %}" role="button">Go to items</a>
{% endif %}

Submitted 3 years, 8 months ago

thanks for the quick reply it returned Reverse for 'designer-posts' with arguments '('',)' not found. 1 pattern(s) tried: ['user/(?P<username>[^/]+)$']

- ahmed.hisham87 3 years, 8 months ago

ok i think you can remove the item.designer from html and make sure its pointing to correct URL and validate it.

- Vengat 3 years, 8 months ago

same error Reverse for 'designer-posts' with no arguments not found. 1 pattern(s) tried: ['user/(?P<username>[^/]+)$']

- ahmed.hisham87 3 years, 8 months ago

show me the url pattern for this {% url 'core:designer-posts' %}. Looke like your urlpattern is wrong, its not routing to correct url

- Vengat 3 years, 8 months ago


Latest Blogs