Thursday, 15 March 2012

Django / HTML - are forms the only way to send POST requests? -



Django / HTML - are forms the only way to send POST requests? -

i'm reading django tutorial , in tutorial, urls.py this:

(r'^vote/$', bookmark_vote_page),

and there model called 'sharedbookmark':

class sharedbookmark(models.model): bookmark = models.foreignkey(bookmark, unique=true) votes = models.integerfield(default=1) users_voted = models.manytomanyfield(user)

but in template, link leads /vote/ this:

{% if shared_bookmarks %} <ul class="bookmarks"> {% shared_bookmark in shared_bookmarks %} <li> <a href="/vote/?id={{ shared_bookmark.id }}" class="vote">[+]</a>

the view handles link this:

@login_required def bookmark_vote_page(request): if request.get.has_key('id'): #if request try: id = request.get['id'] shared_bookmark = sharedbookmark.objects.get(id=id) #if bookmark found shared_bookmark.votes += 1 #make alter 'votes' field in db shared_bookmark.users_voted.add(request.user) #make alter in 'users_voted' field in db shared_bookmark.save()

as can see, template appends '?id=x' (where x number) end of url , view uses request , makes alter database. read, should utilize post requests if want modify database. there way me send post request rather request without creating entire html form / submit button?

you right, should utilize post request if want alter info on server.

if you're using html, need create form , submit button. if using javascript, add together click handler link, submits form. ui

html django post get request

No comments:

Post a Comment