2016-03-03 68 views
-1

index.html中有2個html div,綠色和紅色。django如何將數據從一個函數發佈到其他函數?

我想要當我點擊 每個div,/result頁面出現與消息you have choosed {{ color }} div

我應該如何修改這個:

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

我怎樣才能郵寄的功能數據頁面到在NTER頁面的其他功能?

如何通過點擊潛水將每個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), 
] 

回答

2

好吧,我發現自己的答案。

base.html文件應該改變,因爲這些:

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

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

而且views.py應該這樣改變:

def result(request): 
if 'q' in request.GET: 
    color = request.GET['q'] 
return render(request, 'result.html', {'color': color}) 
相關問題