简体   繁体   中英

Django : Filtering Post by user + UserProfile data

I hope you're well. I got an error: name 'username' is not defined I'd like to have a public page with the user slug.

Ex. profile/louis/ Louis's Post + Louis's UserProfileData

I'm beginning with Django, so I made some mistake.

nutriscore/models.py

class Post(models.Model):
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')

user/models.py

class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)

user/views.py

#public profile        
@login_required(login_url='/earlycooker/login')
def userpublicpostview(request, slug):
    template = 'user_public_profile.html'
    user = User.objects.filter(username).values()
    user_id = user[0]['id'] 
    userprofile = UserProfile.objects.filter(user_id=userprofile).values()  
    user_post = Post.objects.filter(author = user_id, slug=slug) 
    return render(request, template, {'user_posts':user_posts,'userpublic': userpublic})

user/urls.py

path('profile/<slug:slug>/', userpublicpostview,name="user_public_cookwall"),

Use these lines:

user = User.objects.filter(username=slug).values()
...
userprofile = UserProfile.objects.filter(user_id=user_id).values()  
user_post = Post.objects.filter(author = user_id)  # removed slug from here
...
return render(request, template, {'user_posts':user_posts,'userpublic': userprofile})

An efficient version

view

from django.shortcuts import get_object_or_404

@login_required(login_url='/earlycooker/login')
def userpublicpostview(request, slug):
    template = 'user_public_profile.html'
    user = get_object_or_404(User.objects.prefetch_related('blog_posts'), username=slug)
    return render(request, template, {'current_user':user})

template

# user profile information

{{ current_user.userprofile.bio }}
{{ current_user.userprofile.profile_pic }}

# posts

{% for post in current_user.post_set.all %}
  {{ post.title }}
{% endfor %}

You can see here that I can access Profile information via current_user.userprofile because of OneToOne relation between User model and UserProfile model in template. Secondly, I can access all the posts of a given user by makeing reverse query current_user.post_set.all and iterate through the posts. More information on reverse relation(related objects) can be found in documentation .

You can simplify your code :

from django.shortcuts import get_object_or_404

@login_required(login_url='/earlycooker/login')
def userpublicpostview(request, slug):
    user = get_object_or_404(User.objects.select_related('userprofile'), username=slug)
    posts = Post.objects.filter(author_id=user.id) 
    return render(request, 'user_public_profile.html', {
        'user_posts': posts,
        'userpublic': user.userprofile
    })

And in your html, display posts this way :

{% for post in user_posts %}
    <a href=‘{% url ‘user:user_public_cookwall’ post.author.slug %}’>{{ post.title }}</a>
{% endfor %}

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