Skip to content

Commit 511fa87

Browse files
committed
Add Create and Update Post functinality
1 parent 3cdfcc7 commit 511fa87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+70
-5
lines changed

blog/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.db import models
22
from django.utils import timezone
33
from django.contrib.auth.models import User
4+
from django.urls import reverse
45

56
class Post(models.Model):
67
title = models.CharField(max_length=100)
@@ -9,4 +10,7 @@ class Post(models.Model):
910
author = models.ForeignKey(User, on_delete=models.CASCADE)
1011

1112
def __str__(self):
12-
return self.title
13+
return self.title
14+
15+
def get_absolute_url(self):
16+
return reverse('post-detail', kwargs={'pk': self.pk })

blog/templates/blog/post_detail.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends "blog/base.html" %}
2+
{% block content %}
3+
<article class="media content-section">
4+
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
5+
<div class="media-body">
6+
<div class="article-metadata">
7+
<a class="mr-2" href="#">{{ object.author }}</a>
8+
<small class="text-muted">{{ object.date_posted | date:"F d, Y" }}</small>
9+
</div>
10+
<h2 class="article-title">{{ object.title }}</h2>
11+
<p class="article-content">{{ object.content }}</p>
12+
</div>
13+
</article>
14+
{% endblock content %}

blog/templates/blog/post_form.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% extends "blog/base.html" %}
2+
{% load crispy_forms_tags %}
3+
{% block content %}
4+
<div class="content-section">
5+
<form method="POST">
6+
{% csrf_token %}
7+
<fieldset class="form-group">
8+
<legend class="border-button mb-4">Blog Post</legend>
9+
{{ form|crispy }}
10+
</fieldset>
11+
<div class="form-group">
12+
<button class="btn btn-outline-info" type="submit">Post</button>
13+
</div>
14+
</form>
15+
</div>
16+
{% endblock content %}

blog/urls.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from django.urls import path
2-
from .views import PostListView, PostDetailView
2+
from .views import (
3+
PostListView,
4+
PostDetailView,
5+
PostCreateView,
6+
PostUpdateView)
37
from . import views
48

59
urlpatterns = [
610
path('', PostListView.as_view(), name='blog-home'),
711
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
12+
path('post/new', PostCreateView.as_view(), name='post-create'),
13+
path('post/<int:pk>/update', PostUpdateView.as_view(), name='post-update'),
814
path('about/', views.about, name='blog-about'),
9-
10-
]
15+
]

blog/views.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from django.shortcuts import render
22
from django.http import HttpResponse
3-
from django.views.generic import ListView, DetailView
3+
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
4+
from django.views.generic import (
5+
ListView,
6+
DetailView,
7+
CreateView,
8+
UpdateView)
49
from .models import Post
510

611

@@ -19,6 +24,27 @@ class PostListView(ListView):
1924
class PostDetailView(DetailView):
2025
model = Post
2126

27+
class PostCreateView(LoginRequiredMixin, CreateView):
28+
model = Post
29+
fields = ['title', 'content']
30+
31+
def form_valid(self, form):
32+
form.instance.author = self.request.user
33+
return super().form_valid(form)
34+
35+
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
36+
model = Post
37+
fields = ['title', 'content']
38+
39+
def form_valid(self, form):
40+
form.instance.author = self.request.user
41+
return super().form_valid(form)
42+
43+
def test_func(self):
44+
post = self.get_object()
45+
if self.request.uesr == post.author:
46+
return True
47+
return False
2248

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

db.sqlite3

0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)