简体   繁体   中英

How to return multiple items in a get_queryset function in Django?

So I have a web app where the user can enter their information, and eventually I want to display all of it, but at the moment this code right here

class UserPostListView(ListView):
    model = Post
    template_name = 'mainapp/user_posts.html'
    context_object_name = 'posts'

    def get_queryset(self):
        user = get_object_or_404(User,username=self.kwargs.get('username'))
        first_name = get_object_or_404(User,first_name=self.kwargs.get('first_name'))
        return Post.objects.filter(author=user).order_by('-published_date')

It gives me an error, and it says User not found.

I have tried add this to the end of the return statement .order_by('-published_date'),first_name However this did not work.

This is the relevant urls.py file responsible for the user posts

path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),

This is the UserProfileInfo model

class UserProfileInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=50,blank=True,null=True)
    last_name = models.CharField(max_length=50,blank=True,null=True)
    description = models.CharField(max_length=150)
    image = ProcessedImageField(upload_to='profile_pics',
                                           processors=[ResizeToFill(150, 150)],
                                           default='default.jpg',
                                           format='JPEG',
                                           options={'quality': 60})
    joined_date = models.DateTimeField(blank=True,null=True,default=timezone.now)
    verified = models.BooleanField( default=False)

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

And here is the relevant bit of the user-posts.html

<div class="sidebar">
        <p class="active" href="#">{{ view.kwargs.username }}</p>
        <button class="commentbtn"><a class="aclass" href="#">Connect with {{ view.kwargs.username }}</a></button>
        <p>{{ view.kwargs.first_name }}</p>
        <p>Lorem</p>
      </div>

I want to be able to display the first name of the person in the ```{{ view.kwargs.first_name }}, however everything I have tried has failed to work

I expected no errors to occur and for this to work, however this is not the case. Is it possible to have 2 get_queryset methods, or is there something I can do in the current one to achieve my goal of displaying the information of the user

Thanks for any help you can give:)

How about this?

def get_queryset(self):
    qs = super().get_queryset() #this is the same as Post.objects.all()
    user = self.request.user
    return qs.objects.filter(author=user).order_by('-published_date')

Now you can access this query using object_list on your template, but since you changed it's name with context_object_name='posts' , use posts instead:

{% for post in posts %}
<h1>{{ post.first_name }}</h1>
...
{% endfor %}

But it looks like the Post model isn't going to have a first_name column?

You might be better off using the request.user object here as well:

<h1>{{ request.user.first_name }}</h1>
<h2>{{ request.user.username }}</h2>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM