{"action":"created","issue":{"url":"https://api.github.com/repos/NorthBridge/alliance-community/issues/18","labels_url":"https://api.github.com/repos/NorthBridge/alliance-community/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/NorthBridge/alliance-community/issues/18/comments","events_url":"https://api.github.com/repos/NorthBridge/alliance-community/issues/18/events","html_url":"https://github.com/NorthBridge/alliance-community/issues/18","id":107429306,"number":18,"title":"An invalid form submission to index will cause a NameError","user":{"login":"martsberger","id":2503197,"avatar_url":"https://avatars.githubusercontent.com/u/2503197?v=3","gravatar_id":"","url":"https://api.github.com/users/martsberger","html_url":"https://github.com/martsberger","followers_url":"https://api.github.com/users/martsberger/followers","following_url":"https://api.github.com/users/martsberger/following{/other_user}","gists_url":"https://api.github.com/users/martsberger/gists{/gist_id}","starred_url":"https://api.github.com/users/martsberger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martsberger/subscriptions","organizations_url":"https://api.github.com/users/martsberger/orgs","repos_url":"https://api.github.com/users/martsberger/repos","events_url":"https://api.github.com/users/martsberger/events{/privacy}","received_events_url":"https://api.github.com/users/martsberger/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-21T01:26:24Z","updated_at":"2015-09-21T14:40:51Z","closed_at":null,"body":"In apps/shared/views/index.py we have\r\n\r\n```\r\ndef index(request):\r\n    if request.method == 'POST':\r\n        form = ChooseTeamForm(request, request.POST)\r\n        if form.is_valid():\r\n            team = form.cleaned_data['team']\r\n            request.session['team'] = team\r\n    else:\r\n        team = request.session.get('team')\r\n\r\n    if team is None:\r\n        ...\r\n```\r\n\r\nIf the the user POSTs invalid form data, the variable `team` will never be created and the line\r\n`if team is None:`\r\nwill have a NameError.\r\n\r\nI prefer the following:\r\n```\r\ndef index(request):\r\n    form = ChooseTeamForm(request, request.POST or None)\r\n    if form.is_valid():\r\n        team = form.cleaned_data['team']\r\n        request.session['team'] = team\r\n    else:\r\n        team = request.session.get('team')\r\n\r\n    if team is None:\r\n        ...\r\n```\r\n\r\nThis way, a non POST request and an invalid form are both handled by trying to get the team from the session."},"comment":{"url":"https://api.github.com/repos/NorthBridge/alliance-community/issues/comments/142002445","html_url":"https://github.com/NorthBridge/alliance-community/issues/18#issuecomment-142002445","issue_url":"https://api.github.com/repos/NorthBridge/alliance-community/issues/18","id":142002445,"user":{"login":"hectron","id":1649281,"avatar_url":"https://avatars.githubusercontent.com/u/1649281?v=3","gravatar_id":"","url":"https://api.github.com/users/hectron","html_url":"https://github.com/hectron","followers_url":"https://api.github.com/users/hectron/followers","following_url":"https://api.github.com/users/hectron/following{/other_user}","gists_url":"https://api.github.com/users/hectron/gists{/gist_id}","starred_url":"https://api.github.com/users/hectron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hectron/subscriptions","organizations_url":"https://api.github.com/users/hectron/orgs","repos_url":"https://api.github.com/users/hectron/repos","events_url":"https://api.github.com/users/hectron/events{/privacy}","received_events_url":"https://api.github.com/users/hectron/received_events","type":"User","site_admin":false},"created_at":"2015-09-21T14:40:51Z","updated_at":"2015-09-21T14:40:51Z","body":"I like this idea a lot Brad. I think that we should use this approach."}}