Note that there are some explanatory texts on larger screens.

plurals
  1. POPractical Django Projects - Pages 183 and 184
    text
    copied!<p>On pages 183 and 184 there is the following code :</p> <pre><code>def edit_snippet(request, snippet_id): snippet = get_object_or_404(Snippet, pk=snippet_id) if request.user.id != snippet.author.id: return HttpResponseForbidden() if request.method == 'POST': form = SnippetForm(instance=snippet, data=request.POST) if form.is_valid(): snippet = form.save() return HttpResponseRedirect(snippet.get_absolute_url()) else: form = SnippetForm(instance=snippet) return render_to_response('cab/snippet_form.html',{ 'form': form, 'add': False }) edit_snippet = login_required(edit_snippet) </code></pre> <p>Why is it necessary to add a data attribute here :</p> <pre><code>form = SnippetForm(instance=snippet, data=request.POST) </code></pre> <p>Isn't the instance attribute enough ?</p> <p>If the request method isn't POST, then it could be anything but usually it's a GET method. Why is there no data attribute in this case ? Why is it necessary to take into account other request methods ? Couldn't we just write :</p> <pre><code>def edit_snippet(request, snippet_id): snippet = get_object_or_404(Snippet, pk=snippet_id) if request.user.id != snippet.author.id: return HttpResponseForbidden() if request.method == 'POST': form = SnippetForm(instance=snippet, data=request.POST) if form.is_valid(): snippet = form.save() return HttpResponseRedirect(snippet.get_absolute_url()) return render_to_response('cab/snippet_form.html',{ 'form': form, 'add': False }) edit_snippet = login_required(edit_snippet) </code></pre> <p>It sounds more logical to me not to let the user edit his snippet if the request method is not POST. Can you explain me these points ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload