Say I have the following model defined:
class Location(models.Model):
name = models.CharField(max_length=20)
longitude = models.CharField(max_length=30)
latitude = models.CharField(max_length=30)
And there’s an existing Location instance with id=1
I want to post an update against this instance but I am not sure how I should send the pk/id to the server. I have two ideas in mind.
- Include it as part of the url (ex.
^update_location/(?P<pk>[0-9]+)/$'). - Include it in the POST data along with values for updating other attributes.
My question is why would I want to choose one over the other?
I am leaning more towards option 1 because it’s supported, by default, by the generic view django.views.generic.edit.UpdateView that I am using. But then I still don’t know how to choose.
I would say you can do like this:
and then in your view:
and about the form handling you can have a look at :
https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs