Django - objects.all() 什么都不显示

Django - objects.all() shows nothing

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

我只想从请求用户那里得到 'dht node' 的列表,但它在 html 文件中什么也没有显示(好像列表是空的)。我正在使用的用户有 2 'dht nodes',它们显示在 django 管理中。

我不知道哪里出了问题,因为如果我在视图函数中使用指令 "member.dht.create(...)" 并像这样创建一个新的 'dht node',就会显示这个。只有我通过表格输入的 'dht nodes' 没有显示。可以是表格吗?

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

Models.py

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 %}

它什么也没显示,因为你不是 link你dht反对那个UserProfile,所以如果你稍后查询dhts 对于 UserProfile,显然列表是空的。您应该将其添加到 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()
            <b>request.user.userprofile.dht.add(post)</b>
            return redirect('/web/dhtDetail')
    else:
        form = newDHTSensorForm()
    return render(request, 'web/newDhtSensor.html', {'form': form})

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