我正在尝试从模型中获取Django中的对象列表。

我只想从请求用户那里获取“ dht节点”的列表,但是它在html文件中什么也不显示(好像列表为空)。我正在使用的用户有2个“ dht节点”,它们显示在django管理员中。

我不知道这是怎么回事,因为如果我在views函数中使用指令“ member.dht.create(...)”并像这样创建一个新的'dht节点',则会显示出来。我仅通过表单输入的“ dht节点”未显示。可以是表格吗?

非常感谢,这是我的代码:

型号

class Node(models.Model):
    name = models.CharField(primary_key=True, null=False, max_length= 50)
    description= models.CharField(default=None, null=False, max_length= 250)
    topic=models.CharField(default=None, null=False, max_length= 50, unique=True)

    def __unicode__(self):
        return self.name

class dht(Node):
    temp = models.IntegerField(default=None, null=True)
    hum = models.IntegerField(default=None, null=True)

class UserProfile(User):
    uid = models.CharField(default=None, null=False, max_length= 250)
    dht = models.ManyToManyField(dht, blank=True)

    def __unicode__(self):
        return self.user.username


Views.py -dht列表-

@login_required(login_url = '/web/login')
def listDhtSensor(request):
    member = request.user.userprofile
    list = member.dht.all()
    return render(request, 'web/listDhtSensor.html', {'list':list})


HTML -listDhtSensor.html-

{% block content %}
  {% for dht in list %}
      {{ dht.name }}
      {{ dht.topic }}
  {% endfor %}
{% endblock %}


Forms.py

class newDHTSensorForm(forms.ModelForm):
    class Meta:
        model = dht
        field = ['name',
                'description',
                'topic',]
        labels = {'name': 'Name' ,
                    'description': 'Description',
                    'topic': 'Topic',}
        exclude = ['temp', 'hum']


Views.py -dht形式-

@login_required(login_url = '/web/login')
def newDHTSensor(request):
    if request.method == "POST":
        form = newDHTSensorForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('/web/dhtDetail')
    else:
        form = newDHTSensorForm()
    return render(request, 'web/newDhtSensor.html', {'form': form})


HTML -newDhtSensor.html-

{% block content %}
<div class="boxReg">
<form method="post">
  {% csrf_token  %}
  <h2>{{ form.name.errors.as_text }}</h2>
    <p><label for="id_name">Name:</label> <input class="barraForm" type="text" name="name" maxlength="150" autofocus="" required="" id="id_name"></p>
    <p><label for="id_description">Description:</label> <input class="barraForm"  type="text" name="description" maxlength="150" id="id_description"></p>
  <h2>{{ form.topic.errors.as_text }}</h2>
    <p><label for="id_topic">Topic:</label> <input class="barraForm"  type="text" name="topic" maxlength="254" id="id_topic"></p>
  <div class="boxButtonReg">
      <button class="buttonReg" type="submit">Save</button>
  </div>
</form>
</div>
{% endblock %}

最佳答案

它没有显示任何内容,因为您没有将dht对象链接到该UserProfile,因此如果您以后查询该dhtUserProfile,则该列表显然为空。您应该将其添加到dht关系中,例如:

@login_required(login_url = '/web/login')
def newDHTSensor(request):
    if request.method == "POST":
        form = newDHTSensorForm(request.POST)
        if form.is_valid():
            post = form.save()
            request.user.userprofile.dht.add(post)
            return redirect('/web/dhtDetail')
    else:
        form = newDHTSensorForm()
    return render(request, 'web/newDhtSensor.html', {'form': form})


请注意,您首先需要保存post,因此您应省略commit=False aprameter。

10-04 22:02