From ec7d2a820ab2779fd07c0bb2b1b2ecfa01b74620 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 11 Oct 2016 11:04:42 +0100 Subject: [PATCH] Post adding and editing forms --- blog/forms.py | 9 +++++++++ blog/templates/blog/base.html | 3 +++ blog/templates/blog/post_detail.html | 3 +++ blog/templates/blog/post_edit.html | 9 +++++++++ blog/urls.py | 2 ++ blog/views.py | 30 +++++++++++++++++++++++++++- 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 blog/forms.py create mode 100644 blog/templates/blog/post_edit.html diff --git a/blog/forms.py b/blog/forms.py new file mode 100644 index 0000000..c00ad62 --- /dev/null +++ b/blog/forms.py @@ -0,0 +1,9 @@ +from django import forms +from .models import Post + +class PostForm(forms.ModelForm): + + class Meta: + model = Post + fields = ('title', 'text') + \ No newline at end of file diff --git a/blog/templates/blog/base.html b/blog/templates/blog/base.html index 4603b49..65d4c46 100644 --- a/blog/templates/blog/base.html +++ b/blog/templates/blog/base.html @@ -12,6 +12,9 @@
diff --git a/blog/templates/blog/post_detail.html b/blog/templates/blog/post_detail.html index 18073a9..3cdd850 100644 --- a/blog/templates/blog/post_detail.html +++ b/blog/templates/blog/post_detail.html @@ -7,6 +7,9 @@ {{ post.published_date }}
{% endif %} + {% if user.is_authenticated %} + + {% endif %}

{{ post.title }}

{{ post.text|linebreaksbr }}

diff --git a/blog/templates/blog/post_edit.html b/blog/templates/blog/post_edit.html new file mode 100644 index 0000000..60f0a0a --- /dev/null +++ b/blog/templates/blog/post_edit.html @@ -0,0 +1,9 @@ +{% extends 'blog/base.html' %} + +{% block content %} +

New post

+
{% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} \ No newline at end of file diff --git a/blog/urls.py b/blog/urls.py index d58576a..2a18de7 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -4,4 +4,6 @@ from . import views urlpatterns = [ url(r'^$', views.post_list, name='post_list'), url(r'^post/(?P\d+)/$', views.post_detail, name='post_detail'), + url(r'^post/new/$', views.post_new, name='post_new'), + url(r'^post/(?P\d+)/edit/$', views.post_edit, name='post_edit'), ] \ No newline at end of file diff --git a/blog/views.py b/blog/views.py index f1c9b68..38ae51e 100644 --- a/blog/views.py +++ b/blog/views.py @@ -1,6 +1,7 @@ -from django.shortcuts import render, get_object_or_404 +from django.shortcuts import render, get_object_or_404, redirect from django.utils import timezone from .models import Post +from .forms import PostForm def post_list(request): posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') @@ -9,3 +10,30 @@ def post_list(request): def post_detail(request, pk): post = get_object_or_404(Post, pk=pk) return render(request, 'blog/post_detail.html', {'post': post}) + +def post_new(request): + if request.method == 'POST': + form = PostForm(request.POST) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user + post.published_date = timezone.now() + post.save() + return redirect('post_detail', pk=post.pk) + else: + form = PostForm() + return render(request, 'blog/post_edit.html', {'form': form}) + +def post_edit(request, pk): + post = get_object_or_404(Post, pk=pk) + if request.method == 'POST': + form = PostForm(request.POST, instance=post) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user + post.published_date = timezone.now() + post.save() + return redirect('post_detail', pk=post.pk) + else: + form = PostForm(instance=post) + return render(request, 'blog/post_edit.html', {'form': form}) -- 2.34.1