Editing An Object With HTML Form Instead Of Django Model Form
I am trying to edit my objects using HTML form instead of Django model form. I need to know how to make the view for this (See my incomplete view below in bold) Intro: I have a ve
Solution 1:
You don't need a form in your view if you're manually picking the values from request.POST
. Just create or modify a model instance directly.
You can create a form class that corresponds to the one you're writing manually to the the validation etc. when receiving data (but without using it to display the form). You'll likely need to add {% csrf_token %}
to your html form if you do this (or mark the view csrf exempt).
urls.py
...
(r'^/my/special/url/', views.edit_location),
views.py (manually pulling request.POST parameters, and updating a model):
@login_required
def edit_location(request):
if request.method == 'POST':
#location,created = UserLocation.objects.get_or_create(user=request.user)
location = UserLocation.objects.get_or_create(user=request.user)[0]
location.lat = request.POST.get('LAT') #lat is a field in the model see above
location.lon = request.POST.get('LON') #lon is a field in the model see above
location.point = lat (some operation) lon
location.save()
return redirect("home")
form:
<form method="post" action="/my/special/url/">
<input id="jsLat" type="text" placeholder="latittude" name="LAT">
<input id="jsLon" type="text" placeholder="longitude" name="LON">
<button type="submit" id="submit">Submit</button>
</form>
Post a Comment for "Editing An Object With HTML Form Instead Of Django Model Form"