Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

differentiating between different post requests on the same page in Django views.py

Tags:

python

django

I have a web page that I am looking to be able to modify dynamically with multiple post requests. basically there are two methods that the user can submit text to be uploaded into models; one is through a text input field and the other is through a file upload field. How do I set up my python conditionals to do this? I want to be able to differentiate between the two post request with if and statements. What is the differentiating variable that I should use to tell these two apart. My views.py so far has the text input working.

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})
like image 278
Lucas Noah Avatar asked Aug 24 '13 05:08

Lucas Noah


1 Answers

Submit buttons in HTML have name and value properties. For example if you have:

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

Then in Django you can distinguish the two submit actions by the value of action:

if request.POST['action'] == 'Send':
    # do this
elif request.POST['action'] == 'Hello':
    # do that
like image 72
janos Avatar answered Oct 16 '22 14:10

janos