views.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from django.shortcuts import render
  2. from hackathon.forms import UserForm
  3. from django.contrib.auth import logout
  4. from django.template import RequestContext, loader
  5. from django.contrib.auth import authenticate, login
  6. from django.http import HttpResponse, HttpResponseRedirect
  7. def index(request):
  8. context = {'hello': 'world'}
  9. return render(request, 'hackathon/index.html', context)
  10. def test(request):
  11. return HttpResponse('meow')
  12. def register(request):
  13. # A boolean value for telling the template whether the registration was successful.
  14. # Set to False initially. Code changes value to True when registration succeeds.
  15. registered = False
  16. # If it's a HTTP POST, we're interested in processing form data.
  17. if request.method == 'POST':
  18. # Attempt to grab information from the raw form information.
  19. # Note that we make use of both UserForm and UserProfileForm.
  20. user_form = UserForm(data=request.POST)
  21. # If the two forms are valid...
  22. if user_form.is_valid():
  23. # Save the user's form data to the database.
  24. user = user_form.save()
  25. # Now we hash the password with the set_password method.
  26. # Once hashed, we can update the user object.
  27. user.set_password(user.password)
  28. user.save()
  29. # Update our variable to tell the template registration was successful.
  30. registered = True
  31. # Invalid form or forms - mistakes or something else?
  32. # Print problems to the terminal.
  33. # They'll also be shown to the user.
  34. else:
  35. print user_form.errors
  36. # Not a HTTP POST, so we render our form using two ModelForm instances.
  37. # These forms will be blank, ready for user input.
  38. else:
  39. user_form = UserForm()
  40. # Render the template depending on the context.
  41. return render(request,
  42. 'hackathon/register.html',
  43. {'user_form': user_form, 'registered': registered} )