David Leonard 10 лет назад
Родитель
Сommit
01ef8d2250
1 измененных файлов с 45 добавлено и 5 удалено
  1. 45 5
      hackathon_starter/hackathon/views.py

+ 45 - 5
hackathon_starter/hackathon/views.py

@@ -1,7 +1,9 @@
-from django.http import HttpResponse
-from django.template import RequestContext, loader
 from django.shortcuts import render
-
+from hackathon.forms import UserForm
+from django.contrib.auth import logout
+from django.template import RequestContext, loader
+from django.contrib.auth import authenticate, login
+from django.http import HttpResponse, HttpResponseRedirect
 
 def index(request):
 	context = {'hello': 'world'}
@@ -10,5 +12,43 @@ def index(request):
 def test(request):
 	return HttpResponse('meow')
 
-def base(request):
-        return render(request,'hackathon/index.html',context)
+def register(request):
+
+    # A boolean value for telling the template whether the registration was successful.
+    # Set to False initially. Code changes value to True when registration succeeds.
+    registered = False
+
+    # If it's a HTTP POST, we're interested in processing form data.
+    if request.method == 'POST':
+        # Attempt to grab information from the raw form information.
+        # Note that we make use of both UserForm and UserProfileForm.
+        user_form = UserForm(data=request.POST)
+
+        # If the two forms are valid...
+        if user_form.is_valid():
+            # Save the user's form data to the database.
+            user = user_form.save()
+
+            # Now we hash the password with the set_password method.
+            # Once hashed, we can update the user object.
+            user.set_password(user.password)
+            user.save()
+
+            # Update our variable to tell the template registration was successful.
+            registered = True
+
+        # Invalid form or forms - mistakes or something else?
+        # Print problems to the terminal.
+        # They'll also be shown to the user.
+        else:
+            print user_form.errors
+
+    # Not a HTTP POST, so we render our form using two ModelForm instances.
+    # These forms will be blank, ready for user input.
+    else:
+        user_form = UserForm()
+
+    # Render the template depending on the context.
+    return render(request,
+            'hackathon/register.html',
+            {'user_form': user_form, 'registered': registered} )