Access data associated to a primary key

2017-10-02 Thread tango ward
Hi guys, I just want to know how to access and load the data associated to
a primary key in a template.

models.py

class Team_Region(models.Model):
name = models.CharField(max_length=50)

# String representation
def __str__(self):
return self.name



class Team_Name(models.Model):
t_name = models.CharField(max_length=100)
logo = models.ImageField(upload_to='team_logos', blank=True)
region_member = models.ForeignKey(Team_Region, related_name='regions')


def __str__(self):
return self.t_name + ' - ' + str(self.region_member)




views.py

class TeamRegionListView(ListView):
context_object_name = 'regions_listview'
model = Team_Region
template_name = 'dota_teams/team_regions_list.html'


team_regions_list.html

{% block body_block %}


{% for region in regions_listview %}

{{ region.name
}}

{% endfor %}


{% endblock %}


What I want to achieve is to load the teams' logos and names when the
region name link is clicked in the team_regions.html. The teams that will
only be displayed in the template are the teams part of the specific ID
from Team_Region. Say, region is NA, I want all teams under NA to be
displayed in the template.

Not sure how to do this using ListView.

Any suggestions?


TIA

-- 
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/CAA6wQLLOPMnwZKpcXsx99GUsAr2f-mS8W6C_nQd1EA-OHBa_2A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Access data associated to a primary key

2017-10-02 Thread Gourav Chawla
You can create another url, view, template to do that.

Just create a url like : team/id

For the above url create a view, say, teams_under_region which accepts the 
'id'. Based on that id you can then query your database for teams where 
region_member=id. This is just the approach you would follow for functional 
view. But for CBV you have DetailView that takes care of this. Hope this 
helps.

On Monday, October 2, 2017 at 2:53:04 PM UTC+5:30, tangoward15 wrote:
>
> Hi guys, I just want to know how to access and load the data associated to 
> a primary key in a template.
>
> models.py
>
> class Team_Region(models.Model):
> name = models.CharField(max_length=50)
>
> # String representation
> def __str__(self):
> return self.name
>
>
>
> class Team_Name(models.Model):
> t_name = models.CharField(max_length=100)
> logo = models.ImageField(upload_to='team_logos', blank=True)
> region_member = models.ForeignKey(Team_Region, related_name='regions')
>
>
> def __str__(self):
> return self.t_name + ' - ' + str(self.region_member) 
>
>
>
>
> views.py
>
> class TeamRegionListView(ListView):
> context_object_name = 'regions_listview'
> model = Team_Region
> template_name = 'dota_teams/team_regions_list.html'
>
>
> team_regions_list.html
>
> {% block body_block %}
> 
> 
> {% for region in regions_listview %}
> 
> {{ region.name 
> }}
> 
> {% endfor %}
> 
>
> {% endblock %}
>
>
> What I want to achieve is to load the teams' logos and names when the 
> region name link is clicked in the team_regions.html. The teams that will 
> only be displayed in the template are the teams part of the specific ID 
> from Team_Region. Say, region is NA, I want all teams under NA to be 
> displayed in the template.
>
> Not sure how to do this using ListView.
>
> Any suggestions?
>
>
> TIA
>

-- 
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/1b7d75bb-e5ed-4cc5-8b30-7d6487dcb966%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Access data associated to a primary key

2017-10-02 Thread tango ward
Hi Gourav, thanks for the input.

Just a question, Can I iterate through the Team_Region to get the teams
listed under the specific id? or shall I do it in Team_Member class? This
is for the DetailView.

On Mon, Oct 2, 2017 at 9:31 PM, Gourav Chawla <
gauravchawla.chawla...@gmail.com> wrote:

> You can create another url, view, template to do that.
>
> Just create a url like : team/id
>
> For the above url create a view, say, teams_under_region which accepts the
> 'id'. Based on that id you can then query your database for teams where
> region_member=id. This is just the approach you would follow for functional
> view. But for CBV you have DetailView that takes care of this. Hope this
> helps.
>
>
> On Monday, October 2, 2017 at 2:53:04 PM UTC+5:30, tangoward15 wrote:
>>
>> Hi guys, I just want to know how to access and load the data associated
>> to a primary key in a template.
>>
>> models.py
>>
>> class Team_Region(models.Model):
>> name = models.CharField(max_length=50)
>>
>> # String representation
>> def __str__(self):
>> return self.name
>>
>>
>>
>> class Team_Name(models.Model):
>> t_name = models.CharField(max_length=100)
>> logo = models.ImageField(upload_to='team_logos', blank=True)
>> region_member = models.ForeignKey(Team_Region, related_name='regions')
>>
>>
>> def __str__(self):
>> return self.t_name + ' - ' + str(self.region_member)
>>
>>
>>
>>
>> views.py
>>
>> class TeamRegionListView(ListView):
>> context_object_name = 'regions_listview'
>> model = Team_Region
>> template_name = 'dota_teams/team_regions_list.html'
>>
>>
>> team_regions_list.html
>>
>> {% block body_block %}
>>
>> 
>> {% for region in regions_listview %}
>> 
>> {{
>> region.name }}
>> 
>> {% endfor %}
>> 
>>
>> {% endblock %}
>>
>>
>> What I want to achieve is to load the teams' logos and names when the
>> region name link is clicked in the team_regions.html. The teams that will
>> only be displayed in the template are the teams part of the specific ID
>> from Team_Region. Say, region is NA, I want all teams under NA to be
>> displayed in the template.
>>
>> Not sure how to do this using ListView.
>>
>> Any suggestions?
>>
>>
>> TIA
>>
> --
> 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/1b7d75bb-e5ed-4cc5-8b30-7d6487dcb966%40googlegroups.com
> 
> .
> 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/CAA6wQL%2Bm%2BYCeFR8rur2NiStqFW_0NGVEH8S-G3KE7am8BSs1cA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Access data associated to a primary key

2017-10-02 Thread tango ward
Holy .. it works!

I have to access the Team_Name class using the 'related_name=regions' and
from that, I was able to access the logo attributes of Team_Name.

Here's what I did.

{% block body_block %}


{% for region in regions_detail.regions.all %}





{% endfor %}


{% endblock %}


Thanks for your help Gourav. By the way, do you know when should I use CBV
and function view?

On Mon, Oct 2, 2017 at 9:31 PM, Gourav Chawla <
gauravchawla.chawla...@gmail.com> wrote:

> You can create another url, view, template to do that.
>
> Just create a url like : team/id
>
> For the above url create a view, say, teams_under_region which accepts the
> 'id'. Based on that id you can then query your database for teams where
> region_member=id. This is just the approach you would follow for functional
> view. But for CBV you have DetailView that takes care of this. Hope this
> helps.
>
>
> On Monday, October 2, 2017 at 2:53:04 PM UTC+5:30, tangoward15 wrote:
>>
>> Hi guys, I just want to know how to access and load the data associated
>> to a primary key in a template.
>>
>> models.py
>>
>> class Team_Region(models.Model):
>> name = models.CharField(max_length=50)
>>
>> # String representation
>> def __str__(self):
>> return self.name
>>
>>
>>
>> class Team_Name(models.Model):
>> t_name = models.CharField(max_length=100)
>> logo = models.ImageField(upload_to='team_logos', blank=True)
>> region_member = models.ForeignKey(Team_Region, related_name='regions')
>>
>>
>> def __str__(self):
>> return self.t_name + ' - ' + str(self.region_member)
>>
>>
>>
>>
>> views.py
>>
>> class TeamRegionListView(ListView):
>> context_object_name = 'regions_listview'
>> model = Team_Region
>> template_name = 'dota_teams/team_regions_list.html'
>>
>>
>> team_regions_list.html
>>
>> {% block body_block %}
>>
>> 
>> {% for region in regions_listview %}
>> 
>> {{
>> region.name }}
>> 
>> {% endfor %}
>> 
>>
>> {% endblock %}
>>
>>
>> What I want to achieve is to load the teams' logos and names when the
>> region name link is clicked in the team_regions.html. The teams that will
>> only be displayed in the template are the teams part of the specific ID
>> from Team_Region. Say, region is NA, I want all teams under NA to be
>> displayed in the template.
>>
>> Not sure how to do this using ListView.
>>
>> Any suggestions?
>>
>>
>> TIA
>>
> --
> 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/1b7d75bb-e5ed-4cc5-8b30-7d6487dcb966%40googlegroups.com
> 
> .
> 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/CAA6wQLJGX5Ai_L6Cxv0c1BZWpdzwX8Z-pUuHu%3DdaVaxxuwVHyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Access data associated to a primary key

2017-10-03 Thread Gourav Chawla
Glad, you worked it out. You can use them whenever you want. Function based 
views require you to write more code but give you more clarity on what's 
happening. On the other hand CBV help you keep the codebase cleaner.

At the end of the day, it's you who has to decide what to use.

-- 
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/efdec957-131b-4cfd-a0fc-0a37e52d3ea5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Access data associated to a primary key

2017-10-03 Thread tango ward
thanks Gourav. I really appreciate your inputs.

On Tue, Oct 3, 2017 at 9:16 PM, Gourav Chawla <
gauravchawla.chawla...@gmail.com> wrote:

> Glad, you worked it out. You can use them whenever you want. Function
> based views require you to write more code but give you more clarity on
> what's happening. On the other hand CBV help you keep the codebase cleaner.
>
> At the end of the day, it's you who has to decide what to use.
>
> --
> 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/efdec957-131b-4cfd-a0fc-0a37e52d3ea5%40googlegroups.com
> .
> 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/CAA6wQL%2BUGsZ9oQ9Zg7ZaVsNiB1MB2aq2PoYLEX8%2BHX0GpKQZPg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.