2013-08-24 85 views
3

不同的帖子請求我有一個網頁,我希望能夠動態修改多個帖子請求。基本上有兩種方法可以讓用戶提交文本上傳到模型中;一個是通過文本輸入字段,另一個是通過文件上傳字段。我如何設置我的python條件來做到這一點?我希望能夠通過if和語句區分兩個post請求。我應該用什麼區別變量來區分這兩者。我的views.py到目前爲止文本輸入工作。區分Django views.py

def homesite(request): 
corpusitems = CorpusItem.objects.order_by('name') 
if (request.method == 'POST'): 
    f = CorpusItemForm(request.POST) 
    if f.is_valid(): 
     new_corpusitem = f.save() 

return render(request, 'content.html', {'corpusitems': corpusitems}) 
+0

你的邏輯將在'CorpusItemForm'可以去;取決於你想要做什麼。 –

+0

我想添加一個ifelse段到第一個,如果這需要文件上傳後,並將其保存一個變量。我需要能夠用if和if來區分兩個post請求。 –

+0

def homesite(request): corpusitems = CorpusItem.objects.order_by('name') if(request.method =='POST')and ........: f = CorpusItemForm(request.POST) 如果f.is_valid(): new_corpusitem = f.save() 返回渲染(請求, 'content.html',{ 'corpusitems':corpusitems}) –

回答

5

在HTML中提交按鈕具有名稱和值屬性。例如,如果您有:

<form> 
    <input type="submit" name="action" value="Send"/> 
    <input type="submit" name="action" value="Hello"/> 
</form> 

然後在Django,你就可以區分兩個由action值提交操作:

if request.POST['action'] == 'Send': 
    # do this 
elif request.POST['action'] == 'Hello': 
    # do that 
+0

+1 janos。對於在web.py中尋找類似事物的人來說,它幾乎是相同的。使用與上述相同的html,您可以執行'post_args = web.input()',然後執行if(post_args ['action'] =='Send'):#do stuff'。希望對某人有幫助。 –