2014-02-27 52 views
0
def total_rent(request): 
    context = RequestContext(request) 
    venues = Venue.objects.all() 
    decorations = Decoration.objects.all() 
    context1 = ({'venues': venues, 'decorations': decorations}) 
    if request.method == 'GET': 
     rent1 = request.GET.get('venue') 
     rate1 = request.GET.get('decor') 
     if rent1 and rate1: 
      total = int(rent1)+int(rate1) 
      return render_to_response("test.html", context1, {'total':total}) 
     else: 
      return render_to_response("test.html", context1) 

    return render_to_response("test.html", context1, context) 

tryied這對於傳球總回template.html但得到錯誤 「彈出預計將有至少1個參數,得到0」從視圖中發送變量模板Django的

template.html

<form action="" method="get"> 
{% for venue in venues %} 
<input type="radio" name= "venue" id="venue{{ forloop.counter }}" value="{{venue.rent }}"/> 
<label for="venue{{ forloop.counter }}">{{ venue.venue_Name}}</label><br /> 
<img src="{{ venue.picture.url }}" alt="" width="200"> 
{% endfor %} 
{% for decoration in decorations %} 
<input type="radio" name="decor"id="decor{{ forloop.counter }}" value="{{ decoration.rate }}" /> 
<label for="decor{{ forloop.counter }}">{{ decoration.rating }}</label><br /> 
{% endfor %} 
<input type="submit" value="service" /> 
{% if total %} 
    <p>Total cost for venue and decoration: {{ total }}</p> 
{% endif %} 
</form> 

回答

0

保持上下文和上下文1一致。試試這個:

def total_rent(request): 
    context = RequestContext(request) 
    venues = Venue.objects.all() 
    decorations = Decoration.objects.all() 
    context1 = ({'venues': venues, 'decorations': decorations}) 

    if request.method == 'GET': 
     rent1 = request.GET.get('venue') 
     rate1 = request.GET.get('decor') 
     if rent1 and rate1: 
      total = int(rent1)+int(rate1) 
      context1.update({'total': total}) 
      return render_to_response("test.html", context1, context) 
     else: 
      return render_to_response("test.html", context1, context) 

    return render_to_response("test.html", context1, context) 

或者只是:

def total_rent(request): 
    context = RequestContext(request) 
    venues = Venue.objects.all() 
    decorations = Decoration.objects.all() 
    context1 = ({'venues': venues, 'decorations': decorations}) 

    rent1 = request.GET.get('venue') 
    rate1 = request.GET.get('decor') 
    if rent1 and rate1: 
     total = int(rent1)+int(rate1) 
     context1.update({'total': total}) 

    return render_to_response("test.html", context1, context) 

注意request.methodGET默認。