Migdotcom
Migdotcom

Reputation: 72

How to have two separate query-sets under the same class based view

I am trying to have a friends list of all the people who are connected to you with href that sends you to their profile but get:

`Reverse for 'view_profile_with_pk' not found. 'view_profile_with_pk' is not a valid view function or pattern name.

in the HTML traceback details i get :

return render (request, self.template_name, args)

Views.py

class ViewProfile(generic.ListView):
        model = Post
        template_name = 'accounts/profile.html'
        def view_profile(request, pk=None):
            if pk:
                user = User.objects.get(pk=pk)
            else:
                user = request.user
            kwargs = {'user': request.user}
            return render(request, 'accounts/profile.html', kwargs)
    def get(self, request):
        users =User.objects.all()
        object_list= Post.objects.filter(owner =self.request.user).order_by('-timestamp')
        args ={'object_list':object_list,'users':users}
        return render (request, self.template_name, args)

urls.py

 # profilepage
        url(r'^profile/$', views.ViewProfile.as_view(), name='view_profile'),

    # profilepage
    url(r'^profile/(?/P<pk>\d+)/$', views.ViewProfile.as_view(), name='view_profile_with_pk'),

profile.html

    <div class="col-md-4">
        <h1>Friends</h1>
          {%for user in users %}
          <a href="{% url 'accounts:view_profile_with_pk' pk=user.pk  %}">
            <h3>{{user.username}}</h3>
          </a>

          {%endfor%}
    </div>

Upvotes: 1

Views: 61

Answers (1)

Jiaaro
Jiaaro

Reputation: 76878

In your urls.py the following:

url(r'^profile/(?/P<pk>\d+)/$', views.ViewProfile.as_view(), name='view_profile_with_pk'),
#                ^  this character shouldn't be here

should be

url(r'^profile/(?P<pk>\d+)/$', views.ViewProfile.as_view(), name='view_profile_with_pk'),

Upvotes: 1

Related Questions