Working with pk arguments within an included URL

26 views
Skip to first unread message

GavinB841

unread,
Feb 17, 2019, 7:24:53 AM2/17/19
to Django users
Hi,

To briefly explained:
  • I have a main site which provides links to multiple sports club pages.
  • Currently once clicked it opens the club home page and displays information based on that club by passing in the pk.
  • I then have many other pages associated to the clubs. e.g. Teams, Player Registration, Shop etc.
  • But when I click on the navbar for example "Player Registration" I have no idea how to continue the URL with the originally selected PK and how to use that PK in the view.

***Current I have these pages working off the authenticated user but realistically I want it working off the originally selected club***

I am not sure how to write the view to allow for this to work and then pass the pk argument into the nav bar url simiarlary how I did it on the main site:
<a href="{% url 'clubs:club_home_with_pk' pk=club.pk %}">

Would really appreciate any help been stuck on this for a few months now. 

Below is an example of the Clubs Teams I need this to work for:

Urls.py:

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

Nav bar for club pages:

<li><a class="nav-link" href="{% url 'clubs:club_home' %}">Home</a></li>
<li><a class="nav-link" href="{% url 'clubs:teams' %}">Team</a></li>
<li><a class="nav-link" href="{% url 'clubs:pitches' %}">Pitches</a></li>
<li><a class="nav-link" href="{% url 'clubs:memberships' %}">Memberships</a></li>


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):
form = TeamForm()
user = ClubInfo.objects.filter(user=request.user).first()
teams = Team.objects.filter(club_id=user.pk)
return Response({'form': form,
'teams': teams,
})

def post(self, request):
form = TeamForm(data=request.data)
user = ClubInfo.objects.filter(user=request.user).first()
teams = Team.objects.filter(club_id=user.pk)
if form.is_valid():
form.save()
return Response({'form': form,
'teams': teams
})


Simon A

unread,
Feb 17, 2019, 11:10:58 PM2/17/19
to Django users
I think I had a similar scenario a few weeks ago. One option that you have is to store the originally selected PK to the session object. Whenever you load a page, for you can retrieve the PK from the session object. Most likely you'll do this on the get_queryset function

Gavin Boyle

unread,
Feb 18, 2019, 2:10:14 AM2/18/19
to django...@googlegroups.com
Hi Simon, 

That’s a great idea, I’ve only ever worked with built in sessions for logging in. Would you have a link to some documentation or an example that would help me as I’m relatively new to Django and this has been holding me back months now. 

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/KsvZHK1z6BI/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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c508e2ee-d73a-4a02-923d-d033e467efe3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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. 

Simon A

unread,
Feb 18, 2019, 3:10:30 AM2/18/19
to django...@googlegroups.com
Hi Gavin,

Please see this one. https://docs.djangoproject.com/en/2.1/topics/http/sessions/. It first needs to be setup in your settings.py. 

Basically, this is just a field in the database that gets retrieved whenever you want.

# set a session variable
self.request.session['key'] = 'value'

# get a session variable
self.request.session.get('key', None)


For more options, visit https://groups.google.com/d/optout.


--
Kind Regards,
Simon Arriola

Gavin Boyle

unread,
Feb 18, 2019, 4:40:12 AM2/18/19
to django...@googlegroups.com
Perfect Simon. I’ll give it a go. 

Appreciate your help. 

Gavin 


For more options, visit https://groups.google.com/d/optout.

Gavin Boyle

unread,
Feb 21, 2019, 8:31:36 AM2/21/19
to django...@googlegroups.com
Hi Simon. 

Worked perfectly appreciate all your help, such a weight off my shoulders now. 

Thanks 

Gavin
Reply all
Reply to author
Forward
0 new messages