Diplay对象列表,无法正常工作

时间:2017-09-25 12:50:52

标签: django django-models

我想我会问一个非常简单的问题,但这是一个迹象,我仍然没有得到什么。 我有一个团队模型,我想显示登录用户创建的所有团队的列表。

我试过

{% extends 'base.html' %}

{% block body %}
<div class="container">

    <div class="jumbotron">
      <h2>Select one of your team and link it to your project</h2>
    </div>

    <div class="col-md-8 col-md-offset-2">
        {% for i  in team_set.all %}
          <p>{{ i.team_name }}</p>
        {% endfor %}

    </div>


</div>

{% endblock %}

但首先它不会显示任何内容,而是假设显示所有团队,而不仅仅是当前登录用户创建的团队。

请你帮个忙吗?

model.py:

class Team(models.Model):
    team_name = models.CharField(max_length=100, default = '')
    team_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
    members = models.ManyToManyField(MyUser, related_name="members")

    def __str__(self):
        return self.team_name

view.py:

class LinkTeam(TemplateView):
    template_name= 'link_project.html'

url.py:

from django.conf.urls import url, include
from website import views

app_name = 'website'
urlpatterns = [
    url(r'^hr_index/$', views.HRIndex.as_view(), name='hr_index'),
    url(r'^addproject/$', views.ProjectCreate.as_view(), name='add_project'),
    url(r'^addteam/$', views.TeamCreate.as_view(), name='add_team'),
    url(r'^linkteam/$', views.LinkTeam.as_view(), name='Link_team'),
    url(r'^project/(?P<pk>[0-9]+)/$',views.ProjectDetailView.as_view(), name='ProjectDetails'),
]

1 个答案:

答案 0 :(得分:1)

简单地说,你可以试试这个

在视图中

class LinkTeam(TemplateView):

    template_name= 'link_project.html'

    def get(request):
         courts = Yourmodel.objects.all()          # worth looking into?

         return render_to_response(self.template_name, {'courts': courts})

和HTML:

<div class="col-md-8 col-md-offset-2">
    {% for i  in courts %}
      <p>{{ i.team_name }}</p>
    {% endfor %}

</div>