Passing pk arguments from a URL into another view

777 views
Skip to first unread message

GavinB841

unread,
Feb 2, 2019, 11:50:22 AM2/2/19
to Django users
Hi all,

I am having issues using arguments from a URL in another view, I have reached out on stackover flow with no success, i believe i am wording this question poorly.

To quickly explain 
  • I have a main site which shows a list of sports clubs. When the user selects on one it renders that specific club home page using the PK passed in the template.
  • Once I get to this home page e.g. http://127.0.0.1:8000/club_home/2/  .  I have many sub-pages which a user can select but I have no idea how I can use that same PK to filter data in the other pages to only show details based on that club.
  • I would also like to include that PK in the rest of the URLs. e.g. http://127.0.0.1:8000/club_home/2/teams/

Code:

index.html:

<h2>Our Clubs</h2>
{% for club in all_clubs %}
<a href="{% url 'clubs:club_home_with_pk' pk=club.pk %}">
<li>{{ club.club_name }}</li>
</a>
{% endfor %}

urls.py

***I understand I must include the <int:pk>/ before teams in the URL but I am unsure how to pass in that argument***

urlpatterns = [
path('', views.club_home, name='club_home'),
path('<int:pk>/', views.club_home, name='club_home_with_pk'),
path('teams/', views.TeamInfo.as_view(), name='teams'),
]




views.py 


def club_home(request, pk=None):
if pk:
club = ClubInfo.objects.filter(pk=pk)
club_posts = ClubPosts.objects.filter(club_id=club[0])
elif request.user.is_authenticated:
club = ClubInfo.objects.filter(user=request.user)
club_posts = ClubPosts.objects.filter(club_id=club[0])
# photo = model.club_logo.ImageField(storage=profile_pics)
args = {'club': club,
'club_posts': club_posts
}
return render(request, 'club_home_page.html', args)


class TeamInfo(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'teams.html'

def get(self, request):
serializer = TeamSerializer()
user = ClubInfo.objects.filter(user=request.user).first()
teams = Team.objects.filter(club_id=user.pk)
return Response({'serializer': serializer,
'teams': teams,
})


club_main_page.html

***navbar on club home page to get to other pages, I know I need to include <pk> into the URL and I need to pass this argument into the href of this URL but as need to figure out the view before adding this ***


<li><a class="navbar-link" href="{% url 'clubs:teams' %}">Team</a></li>


Any idea what I need to add into the view to allow for this. I would really appreciate any help as I've been stuck on this for weeks now. 

Thanks 

Gavin


Séanadh Ríomhphoist/

Email Disclaimer

Tá an ríomhphost seo agus aon chomhad a sheoltar leis faoi rún agus is lena úsáid ag an seolaí agus sin amháin é. Is féidir tuilleadh a léamh anseo. 

This e-mail and any files transmitted with it are confidential and are intended solely for use by the addressee. Read more here. 

poOya mOsaddegh

unread,
Feb 3, 2019, 9:08:41 AM2/3/19
to django...@googlegroups.com
Hi
You may use include in urls.py
urlpatterns = [
path('', views.club_home, name='club_home'),
    path('<int:pk>/', include(club_home_urls, namespace='club_home')),
]


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3465a2fc-347a-4096-b037-83be9b57d8a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ivan Martić

unread,
Feb 4, 2019, 3:15:55 AM2/4/19
to django...@googlegroups.com
Can you elaborate a bit, i have the same issue with noreverse error

Gavin Boyle

unread,
Feb 4, 2019, 8:58:28 AM2/4/19
to django...@googlegroups.com
Hi Pooya, 

Once I’ve done the include, how can I then pass that PK into the view of the included urls? 

Thanks 
Gavin

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/OR2enuwbwsg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages