Tuesday, 6 August 2013

Django - best practice for getting ID from querystring into edit view

Django - best practice for getting ID from querystring into edit view

I'm wondering what the acceptable best practice is for pulling an id from
a url for use in the edit view. Most example code I see uses slugs, which
I don't need to deal with because SEO is not a concern.
Say I have something like:
def article_edit(request):
if request.method == 'POST': # If the form has been submitted...
#a = get_object_or_404(Article, **?XXX?**)
#a = Article.objects.get(pk=**?XXX?**)
form = ArticleForm(request.POST, instance=a) # A form bound to the
POST data
if form.is_valid(): # All validation rules pass
form.save()
return redirect('/articles/') # Redirect after POST
else:
form = ArticleForm() # An unbound form
return render(request, 'article_form.html', {'form': form})
Where I have commented out two possible options for populating a with an
Article object based on the ID submitted in the POST. The ?XXX? indicates
that I'm not sure how to reference the passed in id.
Any input on those two options, as well as alternative options are
appreciated.

No comments:

Post a Comment