Hi Gavin,

You passed pk from URL.

class TeamInfo(APIView):
    #...
    def get(self, request, pk): # Add pk to params
        club_pk = self.kwargs.get('pk') # get the pk
        #...
        return Response({
                         'club_pk': club_pk
                         })

Will pass the pk into this template.

<li><a class="nav-link" href="{% url 'teams' club_pk %}">Team</a></li>


BR,

Alvaro

On Wed, Mar 13, 2019 at 8:27 AM GavinB841 <gavin.boy...@mail.dcu.ie> wrote:

> Hi Alvaro/Ranganath,
>
> Appreciate your feedback, I understand what you are recommending but I can
> seem to get it working.
>
> Please see the below hopefully it will make more sense:
>
> *View sample for team:*
>
> class TeamInfo(APIView):
>     renderer_classes = [TemplateHTMLRenderer]
>     template_name = 'teams.html'
>
>     def get(self, request):
>         club_pk = request.session.get('pk')
>         form = TeamForm()
>         teams = Team.objects.filter(club_id=club_pk)
>         return Response({'form': form,
>                          'teams': teams,
>                          'club_pk': club_pk
>                          })
>
>
> *Urls: *
>
> urlpatterns = [
>     path('', views.club_home, name='club_home'),
>     path('<int:pk>/', views.club_home, name='club_home_with_pk'),
>     path('<int:pk>/teams/', views.TeamInfo.as_view(), name='teams'),
>     ]
>
>
> *In my nav bar in the template: *
>
> <li><a class="nav-link" href="{% url 'clubs:teams' pk=club_pk 
> %}">Team</a></li>
>
>
> How can I pass that session key argument into this template?
>
> Thanks
>
> Gavin
>
>
> On Sunday, 10 March 2019 14:57:43 UTC, GavinB841 wrote:
>>
>> Hi all,
>>
>> I am not sure if this is possible as I could find nothing online but
>> would appreciate any alternative solution.
>>
>>    - In my view I obtain a pk which is set as a session key
>>    - I need to pass that session key variable into the url argument.
>>
>> e.g. http://127.0.0.1:8000/club_home/<session key>/teams/
>>
>> Code below, any other samples of code needed let me know
>>
>> Thanks
>> Gav
>>
>> *Main Urls*
>>
>> urlpatterns = [
>>     url('admin/', admin.site.urls),
>>     url(r'^club_home/', include('clubkit.clubs.urls'), name='clubs'),
>> ]
>>
>> *Urls.py*
>>
>> urlpatterns = [
>>     path('', views.club_home, name='club_home'),
>>     path('teams/', views.TeamInfo.as_view(), name='teams'),
>>     path('pitches/', views.PitchInfo.as_view(), name='pitches'),
>>     ]
>>
>> *View.py:*
>>
>> def club_home(request, pk=None):
>>     if pk:
>>         request.session['pk'] = pk
>>         club = ClubInfo.objects.filter(pk=pk)
>>         club_posts = ClubPosts.objects.filter(club_id=club[0])
>>     else:
>>         club_pk = request.session.get('pk')
>>         club = ClubInfo.objects.filter(pk=club_pk)
>>         club_posts = ClubPosts.objects.filter(club_id=club[0])
>>     args = {'club': club,
>>             'club_posts': club_posts
>>             }
>>     return render(request, 'club_home_page.html', args)
>>
>>
>> *Séanadh Ríomhphoist/Email DisclaimerTá 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.
>> <https://www4.dcu.ie/iss/seanadh-riomhphoist.shtml>
>> <https://www4.dcu.ie/iss/seanadh-riomhphoist.shtml>This e-mail and any
>> files transmitted with it are confidential and are intended solely for use
>> by the addressee. Read more here.
>> <https://www4.dcu.ie/iss/email-disclaimer.shtml> *
>>
>>
> *Séanadh Ríomhphoist/Email DisclaimerTá 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.
> <https://www4.dcu.ie/iss/seanadh-riomhphoist.shtml>
> <https://www4.dcu.ie/iss/seanadh-riomhphoist.shtml>This e-mail and any
> files transmitted with it are confidential and are intended solely for use
> by the addressee. Read more here.
> <https://www4.dcu.ie/iss/email-disclaimer.shtml> *
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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/7c47d2e6-d1be-49f4-84c2-a12536164105%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/7c47d2e6-d1be-49f4-84c2-a12536164105%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CAN%2BSpDJjdLrLATd27SU7kFmKC7njY3KbfHF96L3dr%3Dg2z_cuOw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to