访问Django的类模型并在模板上显示

时间:2017-10-04 02:03:46

标签: python django

我是django的新手,正在开展我的宠物项目。我在models.py

中的一个类中访问数据时遇到了一些问题

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) 

class Team_Member(models.Model):
    mem_name = models.CharField(max_length=100)
    position = models.CharField(max_length=50)
    member_of_team = models.ForeignKey(Team_Name, related_name='teams')

    def __str__(self):
        return self.mem_name + ' - ' + self.position + ' - ' + str(self.member_of_team) 

views.py

# Display regions in list
class TeamRegionListView(ListView):
    context_object_name = 'regions_list'
    model = Team_Region
    template_name = 'dota_teams/team_regions_list.html'

    # Display all teams associated to the region
class TeamRegionDetailView(DetailView):
    context_object_name = 'region_teams'
    model = Team_Region
    template_name = 'dota_teams/team_regions_detail.html'

class MemberDetailView(DetailView):
    context_object_name = 'team_members'
    model = Team_Name
    template_name = 'dota_teams/member_detail.html'

urls.py

url(r'^$', views.TeamRegionListView.as_view(), name='all_regions'),
url(r'^(?P<pk>\d+)/$', views.TeamRegionDetailView.as_view(),   name='region_teams'),
url(r'^(?P<pk>\d+)/(\d+)/$', views.MemberDetailView.as_view(), name='member_details'),

更新

team_regions_list.html

{% block body_block %}

<div class="row">
    {% for region in regions_list %}
        <div class="col-xs-12 col-lg-4">
            <a href="{{ region.id }}" class="thumbnail" style="width: 350px; height:350px">
                <h4 style="text-align: center; margin-top: 150px"> {{ region.name }} </h4>
            </a>
        </div>
    {% endfor %}
</div>

{%endblock%}

team_regions_detail.html

{% block body_block %}

<div class="row">
    {% for team in region_teams.regions.all %}
        <div class="col-xs-12 col-lg-4">
            <a href="{{ team.id }}" class="thumbnail">
                <img style="width: 100px; height:90px" src="{{ team.logo.url }}" alt="Image not found.">


                <div class="caption">
                    <h4 style="text-align:center">{{ team.t_name }}</h4>
                </div>
            </a>
        </div>
    {% endfor %}
</div>

{%endblock%}

我不确定如何访问Team_Member类下的mem_name和位置变量。在我的views.py中,如果我使用模型Team_Name,则ID未正确分配给区域和团队。我尝试使用Team_Region中的For循环访问Team_Member并使用&#39; related_name&#39;然后,Team_Name访问&#39; related_name&#39; Team_Member但它也不会工作(例如{%for regions_list.regions.teams.all%}}中的成员。我对此有点失落。

有什么建议吗?

TIA

2 个答案:

答案 0 :(得分:0)

根据您的模型,它具有以下关系:

props

您已使用Team_Region --> has many Team_Name Team_Name --> has many Team_Member 定义了外键,如果从related_name访问Team_Member,则必须首先循环Team_Region,然后使用related_name regions_list获取团队名称,然后循环所有团队,使用related_name regions获取团队成员。在您的模板teams中,如下所示:

team_regions_list.html

<强>更新

在您的views.py中,{%for rl in regions_list%} {%for tn in rl.regions.all %} {{tn.teams.all}} {% endfor %} {% endfor %} 对于课程modalTeamRegionDetailView不正确,请更改为:

MemberDetailView

使用# Display regions in list class TeamRegionListView(ListView): context_object_name = 'regions_list' model = Team_Region template_name = 'dota_teams/team_regions_list.html' # Display all teams associated to the region class TeamRegionDetailView(DetailView): context_object_name = 'region_teams' model = Team_Name template_name = 'dota_teams/team_regions_detail.html' class MemberDetailView(DetailView): context_object_name = 'team_members' model = Team_Member template_name = 'dota_teams/member_detail.html' 代替region_teams.teams.all,将region_teams.regions.all更改为:

team_regions_detail.html

更新2: 我认为您错误地管理了上传文件,请参阅official doc

  

默认情况下,Django使用MEDIA_ROOT和本地存储文件   MEDIA_URL设置。

在项目<div class="row"> {% for team in region_teams.teams.all %} <div class="col-xs-12 col-lg-4"> <a href="{{ team.id }}" class="thumbnail"> <img style="width: 100px; height:90px" src="{{ region_teams.logo.url }}" alt="Image not found."> <div class="caption"> <h4 style="text-align:center">{{ team.t_name }}</h4> </div> </a> </div> {% endfor %} </div> 文件中,添加它们:

settings.py

并在项目MEDIA_ROOT = os.path.join(BASE_DIR,'media') MEDIA_URL = '/media/' 文件中添加:

urls.py

之后,模板可以使用(urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) )访问您上传的图片,例如:

/media/team_logos/test.png

答案 1 :(得分:0)

我要感谢Tiny.D对解决方案的想法。

我更新了我的views.py

from django.views.generic import ListView, DetailView
<。>来自.models导入Team_Region,Team_Name,Team_Member

#Display regions in list
class TeamRegionListView(ListView):
context_object_name = 'regionlist'
model = Team_Region
template_name = 'dota_teams/team_region_list.html'



#Display teams associated to the region
class TeamRegionDetailView(DetailView):
context_object_name = 'regiondetail'
model = Team_Region
template_name = 'dota_teams/team_region_detail.html'


#Display members of each team
class TeamDetailView(DetailView):
context_object_name = 'teamdetail'
model = Team_Region
template_name = 'dota_teams/team_detail.html'

urls.py

url(r'^$', views.TeamRegionListView.as_view(), name='region_list'),
url(r'^(?P<pk>\d+)/$', views.TeamRegionDetailView.as_view(), name='region_detail'),
url(r'^(?P<pk>\d+)/(\d+)/$', views.TeamDetailView.as_view(), name='member'),

team_region_list.html和team_region_detail.html的逻辑与我上面的帖子相同。

team_detail.html

{% block body_block %}

<div class="row">
    {% for member in teamdetail.regions.all %}
        {% for member_detail in member.teams.all %}
            <h4>{{ member_detail.mem_name }}</h4>
        {% endfor %}
    {% endfor %}
</div>

{% endblock %}

对于team_detail.html,我只需要执行Tiny.D提到的嵌套循环。现在一切正常。感谢。