Skip to content

Commit

Permalink
Merge pull request #108 from duri0214/Feature/home/create
Browse files Browse the repository at this point in the history
Create機能
  • Loading branch information
duri0214 authored Sep 28, 2024
2 parents abce358 + ca04041 commit 9de36a3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
3 changes: 3 additions & 0 deletions home/static/home/css/post/create.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
padding-top: 48px;
}
3 changes: 3 additions & 0 deletions home/templates/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ <h1 class="display-4">the Portfolio Guidance!</h1>
<li><a class="btn btn-secondary btn-sm" href="#" role="button">XXXX</a></li>
<li><a class="btn btn-secondary btn-sm" href="#" role="button">YYYY</a></li>
</ul>
{% if user.is_authenticated and user.is_superuser %}
<a href="{% url 'home:post_create' %}" class="btn btn-primary">記事を新規作成する</a>
{% endif %}
</div>

<div class="container">
Expand Down
17 changes: 17 additions & 0 deletions home/templates/home/post/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "home/base.html" %}
{% load static %}
{% load humanize %}
{% block extra_css %}
<link rel="stylesheet" href="{% static 'home/css/post/create.css' %}">
{% endblock %}

{% block content %}
<div class="container">
<h2>新規作成</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<article id="post-{{ post.id }}" role="article">
<header>
<h1 itemprop="headline">{{ post.title }}</h1>
{# TODO: superuserのみ #}
<a href="{% url 'home:post_update' pk=post.id %}" class="btn btn-primary">この記事を更新する</a>
{% if user.is_authenticated and user.is_superuser %}
<a href="{% url 'home:post_update' pk=post.id %}" class="btn btn-primary">この記事を更新する</a>
{% endif %}
<a href="{% url 'home:index' %}" class="btn btn-secondary">一覧に戻る</a>
{% if post.image %}
<figure class="eye-catch">
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion home/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import path

from home.views import IndexView, PostDetailView, PostUpdateView
from home.views import IndexView, PostDetailView, PostUpdateView, PostCreateView

app_name = "home"
urlpatterns = [
path("", IndexView.as_view(), name="index"),
path("post/<int:pk>/", PostDetailView.as_view(), name="post_detail"),
path("post/<int:pk>/update/", PostUpdateView.as_view(), name="post_update"),
path("post/create/", PostCreateView.as_view(), name="post_create"),
]
21 changes: 17 additions & 4 deletions home/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import reverse_lazy
from django.views.generic import TemplateView, DetailView, UpdateView
from django.urls import reverse_lazy, reverse
from django.views.generic import TemplateView, DetailView, UpdateView, CreateView
from markdown import Markdown
from mdx_gfm import GithubFlavoredMarkdownExtension

Expand All @@ -17,7 +17,7 @@ def get_context_data(self, **kwargs):

class PostDetailView(DetailView):
model = Post
template_name = "home/posts/detail.html"
template_name = "home/post/detail.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand All @@ -30,8 +30,21 @@ def get_context_data(self, **kwargs):

class PostUpdateView(UpdateView):
model = Post
template_name = "home/posts/update.html"
template_name = "home/post/update.html"
fields = ["title", "image", "category", "summary", "content"]

def get_success_url(self):
return reverse_lazy("home:post_detail", kwargs={"pk": self.object.pk})


class PostCreateView(CreateView):
model = Post
template_name = "home/post/create.html"
fields = ["title", "image", "category", "summary", "content"]

def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)

def get_success_url(self):
return reverse("home:post_detail", args=[str(self.object.id)])

0 comments on commit 9de36a3

Please sign in to comment.