From ce3b9091f080c2a33a60d23af99de3222e570024 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Tue, 4 Dec 2012 16:44:28 -0500 Subject: User can create account --- accounts/forms.py | 7 +++++++ accounts/urls.py | 1 + accounts/views.py | 25 ++++++++++++++++++++++++- templates/accounts/create.html | 11 +++++++++++ templates/index.html | 2 ++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 accounts/forms.py create mode 100644 templates/accounts/create.html diff --git a/accounts/forms.py b/accounts/forms.py new file mode 100644 index 0000000..abdab79 --- /dev/null +++ b/accounts/forms.py @@ -0,0 +1,7 @@ +from django.contrib.auth.forms import UserCreationForm as _UserForm + + +class UserCreationForm(_UserForm): + + class Meta(_UserForm.Meta): + fields = ("username", "email") diff --git a/accounts/urls.py b/accounts/urls.py index ead707a..08f8f38 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -3,4 +3,5 @@ from django.conf.urls import patterns, include, url urlpatterns = patterns('accounts.views', url(r'^profile/$', 'profile', name='profile'), + url(r'^create/$', 'create_account', name='create'), ) diff --git a/accounts/views.py b/accounts/views.py index 2616923..4a0d3ed 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,5 +1,28 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect +from django.contrib.auth import authenticate, login + +from accounts.forms import UserCreationForm def profile(request): return render(request, "accounts/profile.html") + + +def create_account(request): + form = UserCreationForm() + + if request.method == "POST": + form = UserCreationForm(request.POST) + + if form.is_valid(): + form.save() + + user = authenticate(username=form.cleaned_data["username"], + password=form.cleaned_data["password1"]) + login(request, user) + + return redirect("account:profile") + + return render(request, "accounts/create.html", { + "form": form, + }) diff --git a/templates/accounts/create.html b/templates/accounts/create.html new file mode 100644 index 0000000..130f180 --- /dev/null +++ b/templates/accounts/create.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} +

Register an Account

+ +
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} diff --git a/templates/index.html b/templates/index.html index 1e497a5..2bba5df 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,9 +7,11 @@

You've just created your first Django page. Pretty cool, eh?

-- cgit v1.2.3