django如何将函数中的数据发布到其他函数中?

时间:2016-03-03 11:23:19

标签: python django function post

index.html中有2个html div,绿色和红色。

我想点击时 每个div都显示/result页面,其中包含消息you have choosed {{ color }} div

我该如何编辑:

        return render(request, 'result.html', {'color': })

如何将页面中的函数数据发布到页面中的其他函数?

如何通过点击潜水将每个div的颜色发送到结果页面?

views.py;

from django.shortcuts import render
from django.http import HttpResponse

def colors(request):
    return render(request, 'base.html')

def result(request):
    return render(request, 'result.html', {'color': })

base.html文件:

<html>
<head>
<style>
#green {
    width:50px;
    height:50px;
    background:lightgreen;
}
#red {
    margin-top:5px;
    width:50px;
    height:50px;
    background:red;
}
</style>
</head>
<body>

result.html:

you have choosed {{ color }} div

urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from secondv.views import hello, colors, result 

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^index/', colors),
    url(r'^result/', result),
]

1 个答案:

答案 0 :(得分:2)

我自己找到了答案。

base.html应该改为:

(?<=(\w+)\s)?(continue)(?=\s(\w+))?

并且views.py应该像这样改变:

<a href="/result?q=green"> <div id='green'></div> </a>

<a href="/result?q=red"> <div id='red'></div> </a>