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 %}
broto. Should you not be overriding thegetmethod?DetailView, it's even better to overrideget_context_datainstead ofget. See my answer