如何在django 2模板中查询主键对象

时间:2018-04-25 09:59:36

标签: django django-templates django-views

查询我的数据库后,我的模板没有显示任何内容。请查找附上我的模型,网址,模板

MODEL

SqlConnection conn = new SqlConnection();
try
{
    conn.Open();
    /*Your code here, If no Exception is thrown*/
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

视图

   class ScrumyUser(models.Model):
   userRole = (
       ('O', 'Owner'),
       ('A', 'Admin'),
       ('Q', 'Quality Analyst'),
       ('D', 'Developer'),
   )
   fullname = models.CharField(max_length=100)
   role = models.CharField(max_length=1, choices=userRole)



class GoalStatus(models.Model):
   goalStatus = (
       ('P', 'Pending'),
       ('V', 'Verified'),
       ('D', 'Done'),
   )
   status = models.CharField(max_length=1, choices=goalStatus)


class ScrumyGoals(models.Model):
   goalType = (
       ('WG', 'Weekly Goal'),
       ('DT', 'Daily Task'),
   )

   user_id = models.ForeignKey(ScrumyUser, on_delete=models.CASCADE)
   status_id = models.ForeignKey(GoalStatus, on_delete=models.CASCADE)
   goal_type = models.CharField(max_length=2, choices=goalType)
   goal_description = models.TextField()
   date_created = models.DateTimeField('dateCreated')
   date_updated = models.DateTimeField(null=True, blank=True)

模板索引

此模板不会在浏览器上显示任何数据

from django.views import generic
from .models import ScrumyGoals, ScrumyUser, GoalStatus


class IndexView(generic.ListView):
    template_name = 'greyscrumy/index.html'
    context_object_name = 'goals'


    def get_queryset(self):

        return ScrumyGoals.objects.all()

第二个显示但我不知道如何访问SCRUMYUSER和GOALSTATUS

我真的很想知道这一点,我自昨天以来一直在谷歌搜索,并且找不到任何东西

{% if goals %} 
   <h1>{{ object_list.fullname }}</h1> 
   <ul>
   {% for goal in goals.scrumygoals_set.all %}
        <li><a href="{% url 'greyscrumy:index' goal.id %}">{{ goal.goal_type }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No goals are available.</p>
{% endif %} 

1 个答案:

答案 0 :(得分:0)

像这样:

{% for goal in goals %}
    <h1>{{ goal.user_id.fullname }}</h1>
    <p>{{ goal.goal_type }}</p>
    <p>{{ goal.goal_description }}</p>
    <p>{{ goal.date_created }}</p>
    <p>{{ goal.date_updated }}</p>
    <p>{{ goal.user_id.role }}</p>
    <p>{{ goal.status_id.status }}</p>
{% endfor %}
相关问题