Post adding and editing forms
authorNeil Smith <neil.git@njae.me.uk>
Tue, 11 Oct 2016 10:04:42 +0000 (11:04 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Tue, 11 Oct 2016 10:04:42 +0000 (11:04 +0100)
blog/forms.py [new file with mode: 0644]
blog/templates/blog/base.html
blog/templates/blog/post_detail.html
blog/templates/blog/post_edit.html [new file with mode: 0644]
blog/urls.py
blog/views.py

diff --git a/blog/forms.py b/blog/forms.py
new file mode 100644 (file)
index 0000000..c00ad62
--- /dev/null
@@ -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
index 4603b49a1f4eb3151bc5457ea638ce309da9d9f4..65d4c46e919ea3df994a27164a8b5d048c7914c4 100644 (file)
@@ -12,6 +12,9 @@
 
 <body>
        <div class="page-header">
+               {% if user.is_authenticated %}
+                       <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
+               {% endif %}
                <h1><a href="/">Django Girls Blog</a></h1>
        </div>
        <div class="content container">
index 18073a993fd672889e0803c0ac856aacdbf3d2c1..3cdd85041bc526d865b630f3e85a05e507d45766 100644 (file)
@@ -7,6 +7,9 @@
                        {{ post.published_date }}
                </div>
        {% endif %}
+       {% if user.is_authenticated %}
+               <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
+       {% endif %}
        <h1>{{ post.title }}</h1>
        <p>{{ post.text|linebreaksbr }}</p>
 </div>
diff --git a/blog/templates/blog/post_edit.html b/blog/templates/blog/post_edit.html
new file mode 100644 (file)
index 0000000..60f0a0a
--- /dev/null
@@ -0,0 +1,9 @@
+{% extends 'blog/base.html' %}
+
+{% block content %}
+<h1>New post</h1>
+<form method="POST" class="post-form">{% csrf_token %}
+       {{ form.as_p }}
+       <button type="submit" class="save btn btn-default">Save</button>
+</form>
+{% endblock %}
\ No newline at end of file
index d58576af277da014b320125d9e6ece59922b4bd2..2a18de74bba134ade0fff0f581f200073f0b4ded 100644 (file)
@@ -4,4 +4,6 @@ from . import views
 urlpatterns = [
        url(r'^$', views.post_list, name='post_list'),
        url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
+       url(r'^post/new/$', views.post_new, name='post_new'),
+       url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
 ]
\ No newline at end of file
index f1c9b6826e28d894697ee9f61aed881d6dcd2983..38ae51ee7e872d349dd2bbfc90cf9203d8256dbb 100644 (file)
@@ -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})