Skip to content

Implement Update Post View Logic and Template #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .models import Post
from django import forms


class PostForm(forms.ModelForm):
class Meta:
model=Post
fields=['title','content']
3 changes: 3 additions & 0 deletions blog/templates/blog/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<li class="nav-item">
<a class="nav-link" href="{% url 'profile' %}">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'create_post' %}">Create Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Logout</a>
</li>
Expand Down
10 changes: 10 additions & 0 deletions blog/templates/blog/create_post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "blog/base.html" %}

{% block content %}
<h1>Create 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 content %}
10 changes: 10 additions & 0 deletions blog/templates/blog/update_post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "blog/base.html" %}

{% block content %}
<h1>Update Post</h1>
<form action="" method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock content %}
2 changes: 2 additions & 0 deletions blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
urlpatterns = [
path("", views.home, name="blog-home"),
path("about/", views.about, name="blog-about"),
path("create-post/", views.create_post, name="create_post"),
path("update-post/<int:pk>/", views.update_post, name="update_post"),
]
43 changes: 40 additions & 3 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.shortcuts import render

from django.shortcuts import render,redirect,get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Post

from .forms import PostForm
from django.contrib import messages

def home(request):
context = {"posts": Post.objects.all()}
Expand All @@ -10,3 +11,39 @@ def home(request):

def about(request):
return render(request, "blog/about.html", {"title": "About"})

@login_required
def create_post(request):
if request.method == 'POST':
form=PostForm(request.POST)
if form.is_valid():
user=form.save(commit=False)
user.author=request.user
user.save()
messages.success(request, 'The post has been created successfully.')
return redirect('home')
else:
messages.error(request, 'Please correct the following errors:')
form=PostForm()
return render(request,'blog/create_post.html',{'form':form})

@login_required
def update_post(request, pk):
post=get_object_or_404(Post, pk=pk)

if request.method == "POST":
form=PostForm(request.POST, instance=post)
if form.is_valid():
user=form.save()
user.author=request.user
user.save()
messages.info(request, "Post has been updated successfully")
# Redirect To Detail Page
return redirect('')

messages.error(request,"Please correct the following error !!")

else :
form=PostForm(instance=post)

return render(request, "blog/update_post.html", {'form':form})