在我的模板中使用一个视图但多个标签

时间:2014-03-04 16:52:49

标签: django django-templates django-views

我很难过,我确信这很简单,但我希望这个视图在单元格中生成九个不同的整数。我不确定让这些与众不同的最佳方法。截至目前,他们都是一样的。任何帮助是极大的赞赏。谢谢!

查看

def randums(request):
    lower = 10**(9-1)
    upper = 10**9 - 1
    result = random.randint(lower, upper)
    context = {'result': result}
    return render(request, 'index.html', context)


def generate_numbers(randums):
    if request.method == 'POST':
        context = generate_numbers()

TEMPLATE

<form action="" method="post">{% csrf_token %}
    <table class="table table-striped table-bordered table-hover"><tbody>
        <tr><td>{{ result }}</td><td>{{ result }}</td><td>{{ result }}</td></tr>
        <tr><td>{{ result }}</td><td>{{ result }}</td><td>{{ result }}</td></tr>
        <tr><td>{{ result }}</td><td>{{ result }}</td><td>{{ result }}</td></tr>
    </tbody></table>

    <button type="submit" class="rndbtn btn btn-primary btn-large">Random Change</button>
</form>

1 个答案:

答案 0 :(得分:1)

创建一个列表并添加9个随机数:

def randums(request):
    lower = 10**(9-1)
    upper = 10**9 - 1
    result = []
    for x in range(0, 9):
        result.append(random.randint(lower, upper))
    context = {'result': result}
    return render(request, 'index.html', context)

并在您查看结果循环