在HTML中显示数据库(Python)中的数据

时间:2019-04-09 11:39:18

标签: python html django

我想显示HTML中保存在数据库中的RandomId,但是我不确定该怎么做。有人知道怎么做吗?

Here is my html
</script> 
<button onclick="myFunction()"><input type="submit" value="Place Order"></button>
<script>
    function myFunction() {
             alert("Order has been placed in the kitchen \n Your order ID is 
             {{% random_id %}}")    
    }
</script>

here is my views.py

RandomId = request.POST.get("RandomId")
RandomId = get_random_string(length=5) #get random string id 
#RandomId = request.POST.get("RandomId")
customerOrder = CustomerOrder(Table_Num=Table_Num , Food=Food, Quantity=Quantity, Message=Message, RandomId=RandomId) #get data into the CustomerOrder db
customerOrder.save() #save data into db

random_id = CustomerOrder.objects.all()

#order_summary = CustomerOrder.objects.filter(Table_Num = request.Table_Num)
#order_summary = CustomerOrder.objects.filter(Table_Num = 15)

return render(request, 'restaurants/customer_page.html', {'random_id': random_id}) #return customer page

2 个答案:

答案 0 :(得分:0)

我们去

在您的html中

{% for id in random_id %}
    {{ id }}
{% endfor %}

答案 1 :(得分:0)

请缩短您的代码至相关部分。并将您的代码分成不同的部分,以使其更易于阅读。像

模板:

<p>Some html here </p>

视图:

def my_view(request):
   # things that happen here

此外,您的代码两次定义了RandomId,并且您似乎正在将未清除的数据插入数据库中。使用表格,使用form.is_valid(),提供您的模型信息,并仔细检查何时使用{{ something_here }}{% something_else_here %}。提示:{{ variable }}{% if variable == 'hello world' %}