Re: Using sessions key variables as a url argument

2019-03-13 Thread GavinB841
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('/', views.club_home, name='club_home_with_pk'),
path('/teams/', views.TeamInfo.as_view(), name='teams'),
]


*In my nav bar in the template: *

Team


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//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 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. 
<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.
For more options, visit https://groups.google.com/d/optout.


Working with pk arguments within an included URL

2019-02-17 Thread GavinB841
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:




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('/', include([
path('home/', views.club_home, name='club_home_with_pk'),
path('teams/', views.TeamInfo.as_view(), name='teams'),
])),


*Nav bar for club pages:*

Home
Team
Pitches
Memberships



*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
 })




-- 
__

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. 
 _
*_

-- 
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/342241f0-cbf9-4a38-b980-139a79d1d7e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to filter out choices in a form based on the logged in user.

2019-02-09 Thread GavinB841
Hi Ricardo,

Really appreciate your reply, I am struggling to understand where I would 
create this property as I am quite new to Django and haven't worked with 
these before.

Is it included in the model/view/form and under which model for the club, 
user or team. 

Thanks for your help.

Gavin 

On Saturday, 9 February 2019 13:40:09 UTC, Ricardo Cataldi wrote:
>
> Hello there Galvin,
>
>  
>
> You can create properties on the users and filter the queryset 
> conditioning to the user. It depends on how many categories of user you 
> have and how these users are supposed to be filtered by those teams.
>
>  
>
> For example, if you have a spatial selection based on user location, you 
> create a property called ‘location_filter’ and set the separation rule on 
> this property, like:
>
>  
>
> @property
>
> def location_filter(self):
>
> location = self.location
>
> filter_rule = 5miles
>
> return [self.location - filter_rule, self.location + 
> filter_rule]
>
>  
>
> and in your form you set a init argument to query those filter rules, in 
> the fashion that you want:
>
>  
>
> def __init__(self, *args, **kwargs):
>
> user = kwargs.pop(‘user’, None)
>
> self.fields[‘club_id’].queryset = 
> Club.objects.filter(place__lte=user.location_filter[1], 
> place__gte=user.location_filter[0])
>
>  
>
> I have not tested this solution, but with some minor code changes it 
> should work like a charm in filtering the club queryset.
>
>  
>
> *From:* django...@googlegroups.com  <
> django...@googlegroups.com > *On Behalf Of *GavinB841
> *Sent:* sábado, 9 de fevereiro de 2019 11:24
> *To:* Django users >
> *Subject:* Trying to filter out choices in a form based on the logged in 
> user.
>
>  
>
> Hi all,
>
>  
>
> If anyone could help or point me in the direction of some documentation 
> for the below problem I am having I would really appreciate it.
>
>  
>
> Just to explain quickly:
>
>1. I have a model called "Team" which contains a foreign key to 
>another model called "Club"
>2. The "Club" model contains a foreign key to the "User" model.
>3. I have a form for the "Team" model which takes in "club_id", 
>currently it lists all the club_id for all users and I want to restrict 
>this.
>4. I am looking to find out how I can auto-populate this field 
>"club_id" based on the logged in user, which I will then hide in the form. 
>
> Below is my forms.py
>
>  
>
> class ClubInfoForm(forms.ModelForm):
> club_address2 = forms.CharField(required=False)
> club_address3 = forms.CharField(required=False)
>
> class Meta():
> model = ClubInfo
> fields = ('club_name', 'club_logo', 'club_address1', 'club_address2',
>   'club_address3', 'club_town', 'club_county', 
> 'club_country',)
>
> def clean_club_name(self):
> club_name = self.cleaned_data['club_name']
> if ClubInfo.objects.filter(club_name=club_name).exists():
> raise ValidationError(_("Club already exists"))
> return club_name
>
>
> class TeamForm(forms.ModelForm):
>
> class Meta():
> model = Team
> fields = ('club_id', 'team_name', 'manager_name')
>
> def __init__(self, *args, **kwargs):
> super(TeamForm, self).__init__(*args, **kwargs)
>
>  
>
>  
>
> models.py
>
>  
>
> class ClubInfo(models.Model):
>
> user = models.OneToOneField(User, on_delete=models.CASCADE)
> club_name = models.CharField(max_length=50, default='', unique=True)
> club_logo = models.ImageField(upload_to='profile_pics', blank=True)
> club_address1 = models.CharField(max_length=30)
> club_address2 = models.CharField(max_length=30, default='')
> club_address3 = models.CharField(max_length=30, default='')
> club_town = models.CharField(max_length=30)
> club_county = models.CharField(max_length=30)
> club_country = models.CharField(max_length=30)
> created_date = models.DateTimeField(default=timezone.now)
>
> def __str__(self):
> return self.club_name
>
>  
>
> class Team(models.Model):
>
> club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
> team_name = models.CharField(max_length=30)
> manager_name = models.CharField(max_l

Trying to filter out choices in a form based on the logged in user.

2019-02-09 Thread GavinB841
Hi all,

If anyone could help or point me in the direction of some documentation for 
the below problem I am having I would really appreciate it.

Just to explain quickly:

   1. I have a model called "Team" which contains a foreign key to another 
   model called "Club"
   2. The "Club" model contains a foreign key to the "User" model.
   3. I have a form for the "Team" model which takes in "club_id", 
   currently it lists all the club_id for all users and I want to restrict 
   this.
   4. I am looking to find out how I can auto-populate this field "club_id" 
   based on the logged in user, which I will then hide in the form. 

Below is my forms.py

class ClubInfoForm(forms.ModelForm):
club_address2 = forms.CharField(required=False)
club_address3 = forms.CharField(required=False)

class Meta():
model = ClubInfo
fields = ('club_name', 'club_logo', 'club_address1', 'club_address2',
  'club_address3', 'club_town', 'club_county', 'club_country',)

def clean_club_name(self):
club_name = self.cleaned_data['club_name']
if ClubInfo.objects.filter(club_name=club_name).exists():
raise ValidationError(_("Club already exists"))
return club_name


class TeamForm(forms.ModelForm):

class Meta():
model = Team
fields = ('club_id', 'team_name', 'manager_name')

def __init__(self, *args, **kwargs):
super(TeamForm, self).__init__(*args, **kwargs)



models.py


class ClubInfo(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
club_name = models.CharField(max_length=50, default='', unique=True)
club_logo = models.ImageField(upload_to='profile_pics', blank=True)
club_address1 = models.CharField(max_length=30)
club_address2 = models.CharField(max_length=30, default='')
club_address3 = models.CharField(max_length=30, default='')
club_town = models.CharField(max_length=30)
club_county = models.CharField(max_length=30)
club_country = models.CharField(max_length=30)
created_date = models.DateTimeField(default=timezone.now)

def __str__(self):
return self.club_name


class Team(models.Model):

club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
team_name = models.CharField(max_length=30)
manager_name = models.CharField(max_length=20)


def __str__(self):
return self.team_name


-- 
__

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. 
 _
*_

-- 
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/8188ff6e-bf56-4010-9caf-90d8a294a187%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Passing pk arguments from a URL into another view

2019-02-02 Thread GavinB841
*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:*

Our Clubs
{% for club in all_clubs %}

{{ club.club_name }}

  {% endfor %}


*urls.py*

I understand I must include the / before teams in the URL but I 
am unsure how to pass in that argument

urlpatterns = [
path('', views.club_home, name='club_home'),
path('/', 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 
 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 



Team



*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. 
 _
*_

-- 
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/3465a2fc-347a-4096-b037-83be9b57d8a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.