【问题标题】:Generate objects in django template dynamically在 django 模板中动态生成对象
【发布时间】:2017-08-09 20:00:41
【问题描述】:

我的系统中有 2 个模型:

class Display(models.Model):
    name = models.CharField
    UE = models.CharField
    description

class Register(models.Model):
    temp1 = models.FloatField()
    temp2 = models.FloatField()
    flow = models.FloatField()

我在模板中使用 for 创建显示,但每个显示的值是注册模型中的相应字段。我无法使用 Register 进行循环,因为我只使用行(我不能循环字段)。明白吗?

看看我的代码:

查看:

def main(request):
dp_col = Display.objects.all()
reg = Registers.objects.latest('pk')
context = {
    'dp_col': dp_col,
    'reg':reg
}
return render(request,'operation.html',context)

模板:

{% for dp in dp_col %}
        <div class='col-md-6'>
            <div class="display-content">
                <div class="display-data">
                    <h3 class="text-center display-desc">{{dp.name}}
                         <span>:</span> 
                         <span class="text-center display-value">I need put the value of each field here</span>
                         <span class='display-unit'>  {{dp.UE}}</span>
                    </h3>
                </div>
            </div>
        </div>
    {% empty %}
        <!--colocar alguma coisa aqui, caso não tenha nada no for-->
    {% endfor %}

有什么想法吗? 非常感谢!

【问题讨论】:

    标签: python django templates for-loop


    【解决方案1】:

    这可以通过使用 Django Forms 轻松解决:

    yourapp/forms.py

    from django import forms
    
    class DisplayForm(forms.ModelForm):
        class Meta:
            model = Display
            fields = '__all__'
    

    yourapp/views.py

    from .forms import DisplayForm
    
    def main(request):
        # if this is a POST request we need to process the form data
        if request.method == 'POST':
            # create a form instance and populate it with data from the request:
            form = DisplayForm(request.POST)
            # check whether it's valid:
            if form.is_valid():
                # process the data in form.cleaned_data as required
                # ...
                # redirect to a new URL:
                return HttpResponseRedirect('/thanks/')
    
        # if a GET (or any other method) we'll create a blank form
        else:
            form = DisplayForm()
    
        return render(request, 'operation.html', {'form': form})
    

    在操作.html中:

    <form method="post" action="">
        {{ form }}
    </form>
    

    或者如果你想在每个字段中自定义 html:

    <form method="post" action="">
        {% for field in form %}
           {{ field.label_tag }} {{ field }}
        {% endfor %}
    </form>
    

    参考: https://docs.djangoproject.com/en/1.11/topics/forms/

    【讨论】:

    • 我在第一次调用我的视图时需要这个。这是一个 GET 方法。我这样做是为了避免在模板中重复显示的代码。
    • 在这个链接中查看我的模板:i.stack.imgur.com/16BpN.png title 和 UE 数据来自 Display model,field 的 value 来自 Register Model。在调用视图之前,我进行了一个查询,该查询返回寄存器表中的最后一个寄存器。但我无法迭代这些查询集,因为有 1 个寄存器。
    猜你喜欢
    • 2012-07-08
    • 2015-11-23
    • 2013-03-26
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2013-08-08
    相关资源
    最近更新 更多