From 40254d9df1e1a0be237928b6d0239db3ccfa5702 Mon Sep 17 00:00:00 2001 From: Drew Larson Date: Wed, 25 May 2016 18:46:33 -0600 Subject: [PATCH] Add submit form. --- bikeshop_project/core/urls.py | 4 ++-- bikeshop_project/core/views.py | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bikeshop_project/core/urls.py b/bikeshop_project/core/urls.py index 1863202..a1f694e 100644 --- a/bikeshop_project/core/urls.py +++ b/bikeshop_project/core/urls.py @@ -2,6 +2,6 @@ from django.conf.urls import url from .views import DashboardView, NewMembershipView urlpatterns = [ - url(r'^membership/new/$', NewMembershipView.as_view()), - url(r'^$', DashboardView.as_view()), + url(r'^membership/new/$', NewMembershipView.as_view(), name='new_membership'), + url(r'^$', DashboardView.as_view()) ] diff --git a/bikeshop_project/core/views.py b/bikeshop_project/core/views.py index c94c5a5..332e9a3 100644 --- a/bikeshop_project/core/views.py +++ b/bikeshop_project/core/views.py @@ -1,5 +1,8 @@ +from django.contrib import messages +from django.http import HttpResponseRedirect from django.template.response import TemplateResponse -from django.views.generic import View +from django.views.generic import TemplateView, View +from django.core.urlresolvers import reverse from core.forms import MembershipForm @@ -9,8 +12,19 @@ class DashboardView(View): return TemplateResponse(request, 'dashboard.html') -class NewMembershipView(View): +class NewMembershipView(TemplateView): + template_name = 'membership_form.html' def get(self, request): form = MembershipForm() + return self.render_to_response(dict(form=form)) - return TemplateResponse(request, 'membership_form.html', {'form': form}) + def post(self, request): + form = MembershipForm(request.POST) + + if form.is_valid(): + member = form.save() + messages.add_message(request, messages.SUCCESS, 'Successfully created our newest member, {first} {last}' + .format(first=member.first_name, last=member.last_name)) + return HttpResponseRedirect(reverse('new_membership')) + + return self.render_to_response(dict(form=form))