Wednesday, July 29, 2020

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

Replace Item.objects.filter(designer=user).order_by('-timestamp')  with Item.objects.filter(designer = request.user).order_by('-timestamp')

On Thursday, 30 July 2020 at 09:03:26 UTC+10 coolguy wrote:
Please share the project directory structure. Send a screen shot or write it. Tnx


On Tuesday, July 28, 2020 at 11:14:14 PM UTC-4, Ahmed Khairy wrote:

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/535e1e29-cdc3-4333-b531-dc2aecf368ben%40googlegroups.com.

No comments:

Post a Comment