Skip to content

Commit

Permalink
Create form for create and update post
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii73 committed Jul 6, 2018
1 parent f5e639f commit 2eeb8fb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
14 changes: 14 additions & 0 deletions blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Creating and editing forms."""
from django import forms

from .models import Post


class PostForm(forms.ModelForm):
"""Class form based on the model."""

class Meta:
"""Description metadata."""

model = Post
fields = ('title', 'text',)
7 changes: 6 additions & 1 deletion blog/templates/blog/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
</head>
<body>
<div class="page-header">
<h1><a href="/">{% trans "Django Girls Blog" %}</a></h1>
{% if user.is_authenticated %}
<a href="{% url 'post_new' %}" class="top-menu">
<span class="glyphicon glyphicon-plus"></span>
</a>
{% endif %}
<h1><a href="/">{% trans "Django Girls Blog:" %}</a></h1>
</div>
<div class="content container">
<div class="row">
Expand Down
5 changes: 5 additions & 0 deletions blog/templates/blog/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

{% block content %}
<div class="post">
{% if user.is_authenticated %}
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
<span class="glyphicon glyphicon-pencil"></span>
</a>
{% endif %}
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
Expand Down
13 changes: 13 additions & 0 deletions blog/templates/blog/post_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "blog/base.html" %}

{% load i18n %}

{% block content %}
<h1>{% trans "New post" %}</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">
{% trans "Save" %}
</button>
</form>
{% endblock %}
2 changes: 2 additions & 0 deletions blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
urlpatterns = [
re_path(r'^$', views.post_list, name='post_list'),
re_path(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
re_path(r'^post/new/$', views.post_new, name='post_new'),
re_path(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
]
34 changes: 33 additions & 1 deletion blog/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Views for the blog app."""
from django.shortcuts import get_object_or_404, render
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone

from .forms import PostForm
from .models import Post


Expand All @@ -19,3 +20,34 @@ def post_detail(request, pk):
"""Show the full post on the page."""
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})


def post_new(request):
"""Create new post."""
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):
"""Edit post."""
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 changes: 1 addition & 1 deletion mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

urlpatterns = [
path('admin/', admin.site.urls),
path(r'', include('blog.urls')),
path('', include('blog.urls')),
]

0 comments on commit 2eeb8fb

Please sign in to comment.