0

I am fairly new to django, i have two models in my app MyProfile and MyPost, users have a profile and they can create a post, it's all work but i want to show posts created by perticular user inside their own profile i tried adding a user_posts object in MyProfile with a filter author but nothing happened.

MyView

@method_decorator(login_required, name="dispatch") class MyProfileDetailView(DetailView): model = MyProfile def broto(request): user = request.user user_posts = MyPost.objects.filter(author=request.user).order_by('-cr_date') return render(request, {'user_posts':user_posts,'user': user}) 

Profile page html

{% extends 'base.html' %} {% block content %} <div class="p-5"> <img src="/media/{{myprofile.pic}}" /> <h1 class="myhead2">{{myprofile.name}}</h1> <p><strong>Address: {{myprofile.address}}</strong></p> <p><strong>Phone Number: {{myprofile.phone_no}}</strong></p> <p><strong>Email: {{myprofile.user.email}}</strong></p> <p><strong>About:</strong> {{myprofile.purpose}}</p> <p><strong> Total Donation Recived: {{myprofile.donation_recived}}</strong></p> <hr> <table class="table my-3"> <thead class="thead-dark"> <tr> <th>Title</th> <th>Date</th> <th>Action</th> </tr> </thead> {% for MyPost in user_posts %} <tr> <td>{{MyPost.title}}</td> <td>{{MyPost.cr_date | date:"d/m/y"}}</td> <td> <a class="btn btn-dark btn-sm" href='/covid/mypost/{{n1.id}}'>Read More</a> </td> </tr> {% endfor %} </table> </div> {% endblock %} 
2
  • 1
    I am not particularly knowledgeable about class based views, but I have no idea what you are trying to achieve by calling this method broto. Should you not be overriding the get method? Commented Apr 25, 2020 at 9:19
  • 1
    For a DetailView, it's even better to override get_context_data instead of get. See my answer Commented Apr 25, 2020 at 9:28

1 Answer 1

1

As mentioned in comments, your broto methods is probably never executed, that's why nothing happens.

If you use a DetailView, the best way to extend a context is to override get_context_data method. In your case it could be:

@method_decorator(login_required, name="dispatch") class MyProfileDetailView(DetailView): model = MyProfile def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super().get_context_data(**kwargs) # Add in a QuerySet of all the user posts user_posts = MyPost.objects.filter(author=self.request.user).order_by('-cr_date') context['user_posts'] = user_posts context['user'] = self.request.user return context 

You can read about it here: https://docs.djangoproject.com/en/3.0/topics/class-based-views/generic-display/#adding-extra-context

Sign up to request clarification or add additional context in comments.

4 Comments

hhey @Michal S. thanks for your answer now it makes complete sense. I applied your answer but it's giving me an error name 'request' is not defined.
My mistake, it should be self.request.user. I will edit my answer
S. now it's giving an instance error Cannot query "ahmy": Must be "MyProfile" instance..
Could you show the line of code that gives that error?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.