Mohamed
Mohamed

Reputation: 75

making user post show on profile page

Hey guys I am looking to do something like what this question is but I dont see whats wrong with my code. Something like this question. I'm just trying to make a profile page for my site and I wanted to add all the user posts on the profile page but I just dont seem to be able to get it to work. I tried googling the solution and even tried other stackoverflow questions but couldnt if the right answer. If anyone could help me I would be greatly appreciated. Thanks.

I also know its the same question as the one I linked but below is me trying what the guy in the solution did and I just didnt seem to make it work and been going over it for couple of hours sadly to say but what can I say still learning :p

models.py

from django.db import models
from django.contrib.auth.models import User


def upload_location(posts, filename):
    return ("%s/%s" %(posts.author, filename))

class Post(models.Model):
    title = models.CharField(max_length=200)
    image = models.ImageField(upload_to=upload_location, null=True, blank=True, height_field="height_field", width_field="width_field")
    height_field = models.IntegerField(default=0)
    width_field = models.IntegerField(default=0)
    description = models.TextField()
    pub_date = models.DateTimeField(auto_now=False)
    author = models.ForeignKey(User)
    votes_total = models.IntegerField(default=0)

def __unicode__(self):
    return self.user.username

def pub_date_pretty(self):
    return self.pub_date.strftime('%b %d %Y')

views.py, I am importing my Post class from a different app so I did think that could be the problem.

from posts.models import Post

def view_profile(request):
    logged_in_user = request.user
    logged_in_user_posts = Post.objects.filter(user=author)

    return render(request, 'accounts/profile.html', {'posts': logged_in_user_posts})

html file

<div>
{% for post in posts %}

    Post: {{ post.author }}

    <br>

{% endfor %}
</div>

urls.py

from django.conf.urls import url
from . import views

app_name = 'accounts'

urlpatterns = [
    url(r'^register/', views.signup, name="signup"),
    url(r'^login/', views.loginuser, name="login"),
    url(r'^logout/', views.logoutview, name="logout"),
    url(r'^profile/', views.view_profile, name="viewprofile"),
]

Added after reading first answer posted below

after looking at the following post below I got this error:

FileNotFoundError at /accounts/profile/ [Errno 2] No such file or directory: 'C:\Users\Mohamed\Desktop\thegallerybox\media_cdn\imag‌e

Am I receiving this error because in my html file I'm not calling the user image from the user folder in the media folder. I am uploading the images based on the author shown in my models.py so I thought that might have to do with getting the image in the html file

def upload_location(posts, filename): return ("%s/%s" %(posts.author, filename))

Upvotes: 0

Views: 87

Answers (1)

NS0
NS0

Reputation: 6096

You want to filter based on author since that's what is defined in your Post model, and you want to check against the currently logged in user.

Try making the following change

logged_in_user_posts = Post.objects.filter(author=logged_in_user)

Upvotes: 1

Related Questions