{"id":87,"date":"2010-03-07T20:42:02","date_gmt":"2010-03-08T00:42:02","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=87"},"modified":"2010-03-07T20:42:02","modified_gmt":"2010-03-08T00:42:02","slug":"is-short-sweet","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2010\/03\/is-short-sweet\/","title":{"rendered":"Is Short Sweet?"},"content":{"rendered":"<p>I was reading this <a href=\"http:\/\/jacobian.org\/writing\/dynamic-form-generation\/\">post by Jacob Kaplan-Moss on Dynamic Form Generation<\/a>, and this little side point caught my interest.  He pointed out a more compact way to write a view for a create operation.<\/p>\n<p>OLD WAY:<\/p>\n<pre>from django.shortcuts import redirect, render_to_response\r\nfrom myapp.forms import UserCreationForm\r\n\r\ndef create_user(request):\r\n    if request.method == 'POST':\r\n        form = UserCreationForm(request.POST)\r\n        if form.is_valid():\r\n            do_something_with(form.cleaned_data)\r\n            return redirect(\"create_user_success\")\r\n    else:\r\n        form = UserCreationForm()\r\n\r\n    return render_to_response(\"signup\/form.html\", {'form': form})<\/pre>\n<p>NEW WAY:<\/p>\n<pre>def create_user(request):\r\n    form = UserCreationForm(request.POST or None)\r\n    if form.is_valid():\r\n        do_something_with(form.cleaned_data)\r\n        return redirect(\"create_user_success\")\r\n\r\n    return render_to_response(\"signup\/form.html\", {'form': form})<\/pre>\n<p>Now, I&#8217;m all for keeping things concise, but I&#8217;m not sure if this new way is as clear.  I want to keep my code readable for those that are tasked with maintaining it someday can understand it.<\/p>\n<p>What do you think? Is the 2nd method OK when others need to read the code?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was reading this post by Jacob Kaplan-Moss on Dynamic Form Generation, and this little side point caught my interest. He pointed out a more compact way to write a view for a create operation. OLD WAY: from django.shortcuts import redirect, render_to_response from myapp.forms import UserCreationForm def create_user(request): if request.method == &#8216;POST&#8217;: form = UserCreationForm(request.POST) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2010\/03\/is-short-sweet\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Is Short Sweet?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":2,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}