diff --git a/home/static/home/css/post/create.css b/home/static/home/css/post/create.css new file mode 100644 index 00000000..532db859 --- /dev/null +++ b/home/static/home/css/post/create.css @@ -0,0 +1,3 @@ +body { + padding-top: 48px; +} diff --git a/home/templates/home/index.html b/home/templates/home/index.html index bc167c11..6b2bfc7b 100644 --- a/home/templates/home/index.html +++ b/home/templates/home/index.html @@ -14,6 +14,9 @@

the Portfolio Guidance!

  • XXXX
  • YYYY
  • + {% if user.is_authenticated and user.is_superuser %} + 記事を新規作成する + {% endif %}
    diff --git a/home/templates/home/post/create.html b/home/templates/home/post/create.html new file mode 100644 index 00000000..f999d588 --- /dev/null +++ b/home/templates/home/post/create.html @@ -0,0 +1,17 @@ +{% extends "home/base.html" %} +{% load static %} +{% load humanize %} +{% block extra_css %} + +{% endblock %} + +{% block content %} +
    +

    新規作成

    +
    + {% csrf_token %} + {{ form.as_p }} + +
    +
    +{% endblock %} diff --git a/home/templates/home/posts/detail.html b/home/templates/home/post/detail.html similarity index 80% rename from home/templates/home/posts/detail.html rename to home/templates/home/post/detail.html index 0b434523..95e6f18a 100644 --- a/home/templates/home/posts/detail.html +++ b/home/templates/home/post/detail.html @@ -9,8 +9,9 @@

    {{ post.title }}

    - {# TODO: superuserのみ #} - この記事を更新する + {% if user.is_authenticated and user.is_superuser %} + この記事を更新する + {% endif %} 一覧に戻る {% if post.image %}
    diff --git a/home/templates/home/posts/update.html b/home/templates/home/post/update.html similarity index 100% rename from home/templates/home/posts/update.html rename to home/templates/home/post/update.html diff --git a/home/urls.py b/home/urls.py index 1bcd0593..10ff415d 100644 --- a/home/urls.py +++ b/home/urls.py @@ -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//", PostDetailView.as_view(), name="post_detail"), path("post//update/", PostUpdateView.as_view(), name="post_update"), + path("post/create/", PostCreateView.as_view(), name="post_create"), ] diff --git a/home/views.py b/home/views.py index 905ec14b..314d2098 100644 --- a/home/views.py +++ b/home/views.py @@ -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 @@ -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) @@ -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)])