Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法分析余数:';[工厂id]';从';总生态足迹[工厂id]';_Python_Django - Fatal编程技术网

Python 无法分析余数:';[工厂id]';从';总生态足迹[工厂id]';

Python 无法分析余数:';[工厂id]';从';总生态足迹[工厂id]';,python,django,Python,Django,我的视图.py: def home(request): if not request.user.is_authenticated(): template = 'data/login.html' return render(request, template) else: plants = Plant.objects.filter(user=request.user) total_ecof = []

我的视图.py

def home(request):
    if not request.user.is_authenticated():
        template = 'data/login.html'
        return render(request, template)
    else:
            plants = Plant.objects.filter(user=request.user)
            total_ecof = []
            total_ectr = []
            for plant in plants:
                x = 0
                y = 0
                for recording in Recording.objects.filter(plant__id=plant.id):
                    x += (recording.time_to_detect * recording.performance_loss) / (
                    plant.nominal_power * recording.years_of_operation)
                    y += ((10 + 15 + 200 + 50) * plant.no_modules + 240 * 50 * recording.occurrence * plant.no_modules) / (
                         plant.nominal_power * 3)
                total_ecof.append(x)
                total_ectr.append(y)
        template = 'data/home.html'
        context = {
                    'plants':plants,
                    'total_ecof':total_ecof,
                    'total_ectr':total_ectr,
                    }
        return render(request, template, context)
def home(request):
    if not request.user.is_authenticated():
        template = 'data/login.html'
        return render(request, template)
    else:
            plants = Plant.objects.filter(user=request.user)
            for plant in plants:
                plant.total_ecof = 0
                for recording in Recording.objects.filter(plant__id=plant.id):
                    plant.total_ecof += (recording.time_to_detect * recording.performance_loss) / (plant.nominal_power * recording.years_of_operation)
        template = 'data/home.html'
        context = {
                    'plants':plants
                    }
        return render(request, template, context)
{% for plant in plants %}
    <tr>
       <td>{{ plant.management_company }}</td>
       <td>{{ plant.plant_name }}</td>
       <td>{{ plant.total_ecof }}</td>
    </tr>
{% endfor %}
我的html模板

{% for plant in plants %}
    <tr>
       <td>{{ plant.management_company }}</td>
       <td>{{ plant.plant_name }}</td>
       <td>{{total_ecof[plnat.id]}}</td>
    </tr>
{% endfor %}
我不明白为什么。我做错了什么


此外,请您帮助我创建一个条形图,其中x轴是植物,y轴是相应的总生态足迹

不要为总数创建单独的列表。创建一个由(记录,总计)对组成的列表,或者直接在记录实例上设置总计。

@Exprator和@Daniel感谢您的评论。我找到了解决办法:

首先,在我的models.py中,我在课堂上添加了以下内容:

def total_ecof(self):
    return self
然后我更改了我的视图。py

def home(request):
    if not request.user.is_authenticated():
        template = 'data/login.html'
        return render(request, template)
    else:
            plants = Plant.objects.filter(user=request.user)
            total_ecof = []
            total_ectr = []
            for plant in plants:
                x = 0
                y = 0
                for recording in Recording.objects.filter(plant__id=plant.id):
                    x += (recording.time_to_detect * recording.performance_loss) / (
                    plant.nominal_power * recording.years_of_operation)
                    y += ((10 + 15 + 200 + 50) * plant.no_modules + 240 * 50 * recording.occurrence * plant.no_modules) / (
                         plant.nominal_power * 3)
                total_ecof.append(x)
                total_ectr.append(y)
        template = 'data/home.html'
        context = {
                    'plants':plants,
                    'total_ecof':total_ecof,
                    'total_ectr':total_ectr,
                    }
        return render(request, template, context)
def home(request):
    if not request.user.is_authenticated():
        template = 'data/login.html'
        return render(request, template)
    else:
            plants = Plant.objects.filter(user=request.user)
            for plant in plants:
                plant.total_ecof = 0
                for recording in Recording.objects.filter(plant__id=plant.id):
                    plant.total_ecof += (recording.time_to_detect * recording.performance_loss) / (plant.nominal_power * recording.years_of_operation)
        template = 'data/home.html'
        context = {
                    'plants':plants
                    }
        return render(request, template, context)
{% for plant in plants %}
    <tr>
       <td>{{ plant.management_company }}</td>
       <td>{{ plant.plant_name }}</td>
       <td>{{ plant.total_ecof }}</td>
    </tr>
{% endfor %}
和我的html

def home(request):
    if not request.user.is_authenticated():
        template = 'data/login.html'
        return render(request, template)
    else:
            plants = Plant.objects.filter(user=request.user)
            total_ecof = []
            total_ectr = []
            for plant in plants:
                x = 0
                y = 0
                for recording in Recording.objects.filter(plant__id=plant.id):
                    x += (recording.time_to_detect * recording.performance_loss) / (
                    plant.nominal_power * recording.years_of_operation)
                    y += ((10 + 15 + 200 + 50) * plant.no_modules + 240 * 50 * recording.occurrence * plant.no_modules) / (
                         plant.nominal_power * 3)
                total_ecof.append(x)
                total_ectr.append(y)
        template = 'data/home.html'
        context = {
                    'plants':plants,
                    'total_ecof':total_ecof,
                    'total_ectr':total_ectr,
                    }
        return render(request, template, context)
def home(request):
    if not request.user.is_authenticated():
        template = 'data/login.html'
        return render(request, template)
    else:
            plants = Plant.objects.filter(user=request.user)
            for plant in plants:
                plant.total_ecof = 0
                for recording in Recording.objects.filter(plant__id=plant.id):
                    plant.total_ecof += (recording.time_to_detect * recording.performance_loss) / (plant.nominal_power * recording.years_of_operation)
        template = 'data/home.html'
        context = {
                    'plants':plants
                    }
        return render(request, template, context)
{% for plant in plants %}
    <tr>
       <td>{{ plant.management_company }}</td>
       <td>{{ plant.plant_name }}</td>
       <td>{{ plant.total_ecof }}</td>
    </tr>
{% endfor %}
{%用于plants in plants%}
{{工厂管理公司}
{{plant.plant_name}
{{plant.total_ecof}
{%endfor%}

我不知道这是否是最好的解决方案,但它正在发挥作用:)谢谢你们

在模板中,您引用了total_ecof[plnat.id]。是的,但我如何解决这个问题?例如,我需要的是plant.id=3,然后是total_ecof[3],正如所有其他答案和评论中所解释的,以及文档中明确说明的那样,您不能在模板中执行类似操作。