2015-12-17 42 views
0

編輯:在django中添加兩個數

我是新來的Django,我需要添加兩個數字x和y。

x和y是來自用戶的輸入。

這裏是我的views.py

from django.http import HttpResponseRedirect 
from django.http import HttpResponse 
from django.shortcuts import render 
from .forms import InputForm 

def add(request): 
    if request.method == 'POST':  # If the form has been submitted... 
    form = InputForm(request.POST)  # A form bound to the POST data 
    if form.is_valid():    # All validation rules pass 
     cd = form.cleaned_data  # Process the data in form.cleaned_data 
     input1 = cd['x'] 
     input2 = cd['y'] 
     output = input1 + input2 
     return HttpResponseRedirect(request,'/thanks/')# Redirect to new url 
    else: 
     form = InputForm() # An unbound form 


    return render(request, 'scraper/base.html', {'form': form })  


def thanks(request,output): 
return render(request, 'scraper/base.html', output) 

這裏是我的forms.py

from django import forms 

class InputForm(forms.Form): 
    x = forms.IntegerField(label='Value of x') 
    y = forms.IntegerField(label='Value of y') 

這裏是我的urls.py

from django.conf.urls import url 
from . import views 

urlpatterns = [ 
url(r'^$', views.add, name='add'), 
url(r'^/', views.thanks , name='thanks'), 
] 

這裏是output.html

<html> 
    <head> 
    <title>Thanks</title> 
    </head> 
    <body> 
    <h1>Thanks</h1> 
    <h1> Output = {{output}} </h1> 
    </body> 
    </html> 

用戶輸入x和y值,然後點擊添加按鈕輸出應該顯示在一個新的頁面,如output.html那麼該怎麼做?

我知道在views.py中有一些錯誤,我正在學習Django。請指出並告訴我實施這種方法的正確方法

我掙扎了5個小時。

+0

請問你的urls.py是什麼樣子?根據錯誤信息有錯誤 – fodma1

+1

請分享您的urls.py – utkbansal

+0

嘗試:'HttpResponse('Result:{}'.format(output))''而不是'HttpResponseRedirect(...)' – ahmed

回答

1

我認爲你正在使用的重定向錯誤,你不能在HttpResponseRedirect發送參數,這裏檢查的修改,並採取看在documentation更多的解釋:

views.py

def add(request): 
    if request.method == 'POST':  # If the form has been submitted... 
    form = InputForm(request.POST)  # A form bound to the POST data 
    if form.is_valid():    # All validation rules pass 
     cd = form.cleaned_data  # Process the data in form.cleaned_data 
     input1 = cd['x'] 
     input2 = cd['y'] 
     output = input1 + input2 
     return HttpResponseRedirect('/thanks/{output}/'.format(output=output)) # Redirect to new url 
    else: 
     form = InputForm() # An unbound form 
    return render(request, 'scraper/base.html', {'form': form }) 

urls.py

url(r'^thanks/(?P<output>\d+)/$', thanks), 
+0

再次感謝:) –

2

您可以使用會話來存儲輸出,然後在下一個視圖中檢索它。會話框架允許您以每個站點訪問者爲基礎存儲和檢索任意數據。

編輯您的views.py如下 -

def add(request): 
    if request.method == 'POST':   
     form = InputForm(request.POST)  
     if form.is_valid():     
      cd = form.cleaned_data  
      input1 = cd['x'] 
      input2 = cd['y'] 
      output = input1 + input2 
      # Save the result in the session 
      request.session['output'] = output 
      return HttpResponseRedirect('/thanks/') 
    else: 
     form = InputForm() 
    return render(request, 'scraper/base.html', {'form': form }) 


def thanks(request): 
    # Get the result from the session 
    output = request.session.pop('output', None) 
    return render(request, 'scraper/output.html', {'output':output}) 

你urls.py應該是 -

urlpatterns = [ 
    url(r'^$', views.add, name='add'), 
    url(r'^thanks/$', views.thanks , name='thanks'), 
] 
+0

您在'thanks'函數的最後一行有一個縮進錯誤。 – utkbansal

+0

@SanjayOrtiz嘗試複製完全相同的代碼。 – utkbansal

+0

更正了它,現在https://dpaste.de/34U2:/ –