diff --git a/digital_glarus/templates/digital_glarus/index.html b/digital_glarus/templates/digital_glarus/index.html index 2e9980b3..912776bb 100644 --- a/digital_glarus/templates/digital_glarus/index.html +++ b/digital_glarus/templates/digital_glarus/index.html @@ -1,9 +1,9 @@ {% if error_message %}

{{ error_message }}

{% endif %} -
-{% csrf_token %} - - + + {% csrf_token %} + {{ form }} +
{% if message_list %} diff --git a/digital_glarus/views.py b/digital_glarus/views.py index 95da6bb8..a4e1c4f2 100644 --- a/digital_glarus/views.py +++ b/digital_glarus/views.py @@ -1,15 +1,13 @@ -from django.shortcuts import render +import datetime from django.shortcuts import get_object_or_404, render - - +from django.forms import ModelForm +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse from .models import Message -def detail(request): - pass - def detail(request, message_id): p = get_object_or_404(Message, pk=message_id) @@ -19,7 +17,26 @@ def detail(request, message_id): def send_message(request): pass +class MessageForm(ModelForm): + class Meta: + model = Message + fields = ['name', 'email', 'phone_number', 'message' ] + def index(request): + if request.method == 'POST': + message = Message(received_date=datetime.datetime.now()) + form = MessageForm(request.POST, instance=message) + + if form.is_valid(): + form.save() + return HttpResponseRedirect(reverse("digital_glarus:index")) + + form = MessageForm() message_list = Message.objects.order_by('-received_date')[:5] - context = { 'message_list': message_list, } + + context = { + 'message_list': message_list, + 'form': form, + } + return render(request, 'digital_glarus/index.html', context)