From 819d554bac053873ba7555ad1086d9aee8993abb Mon Sep 17 00:00:00 2001 From: sdjo Date: Fri, 17 Jan 2025 15:41:11 +0900 Subject: [PATCH] =?UTF-8?q?notice=20=EB=93=B1=EB=A1=9D=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog/forms.py | 4 +- .../0005_rename_content_post_contents.py | 18 +++++++ blog/models.py | 4 +- blog/templates/blog/create_post.html | 2 +- butler/forms.py | 11 ++++ butler/templates/butler/create_notice.html | 50 +++++++++++++++++++ butler/templates/butler/notice_detail.html | 2 +- .../butler/{notice.html => notice_list.html} | 11 +++- butler/urls.py | 3 +- butler/views.py | 21 ++++++-- 10 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 blog/migrations/0005_rename_content_post_contents.py create mode 100644 butler/forms.py create mode 100644 butler/templates/butler/create_notice.html rename butler/templates/butler/{notice.html => notice_list.html} (72%) diff --git a/blog/forms.py b/blog/forms.py index 94a108b..2009822 100644 --- a/blog/forms.py +++ b/blog/forms.py @@ -5,10 +5,10 @@ from .models import Post class PostForm(forms.ModelForm): class Meta: model = Post - fields = ['title', 'content', 'summary', 'tags'] + fields = ['title', 'contents', 'summary', 'tags'] widgets = { 'title': forms.TextInput(attrs={'class': 'form-control'}), - 'content': forms.Textarea(attrs={'class': 'form-control', 'id': 'markdown-editor'}), + 'contents': forms.Textarea(attrs={'class': 'form-control', 'id': 'markdown-editor'}), 'summary': forms.TextInput(attrs={'class': 'form-control'}), 'tags': TagWidget(attrs={'class': 'form-control', 'placeholder': 'Add tags'}), } diff --git a/blog/migrations/0005_rename_content_post_contents.py b/blog/migrations/0005_rename_content_post_contents.py new file mode 100644 index 0000000..3001117 --- /dev/null +++ b/blog/migrations/0005_rename_content_post_contents.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.14 on 2025-01-17 15:36 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('blog', '0004_remove_post_tags_post_tags'), + ] + + operations = [ + migrations.RenameField( + model_name='post', + old_name='content', + new_name='contents', + ), + ] diff --git a/blog/models.py b/blog/models.py index 6d4e085..bd6e088 100644 --- a/blog/models.py +++ b/blog/models.py @@ -5,7 +5,7 @@ from taggit.managers import TaggableManager class Post(models.Model): title = models.CharField(max_length=200) # 제목 - content = models.TextField() # 본문 (마크다운 저장) + contents = models.TextField() # 본문 (마크다운 저장) summary = models.CharField(max_length=2000, blank=True, null=True) # 요약 created_at = models.DateTimeField(auto_now_add=True) # 작성일 updated_at = models.DateTimeField(auto_now=True) # 수정일 @@ -15,7 +15,7 @@ class Post(models.Model): def render_markdown(self): """마크다운을 HTML로 변환""" md = MarkdownIt() - return md.render(self.content) + return md.render(self.contents) def __str__(self): return self.title \ No newline at end of file diff --git a/blog/templates/blog/create_post.html b/blog/templates/blog/create_post.html index 96684b9..2d2e43e 100644 --- a/blog/templates/blog/create_post.html +++ b/blog/templates/blog/create_post.html @@ -25,7 +25,7 @@

contents

- {{ form.content }} + {{ form.contents }}
diff --git a/butler/forms.py b/butler/forms.py new file mode 100644 index 0000000..f0281a1 --- /dev/null +++ b/butler/forms.py @@ -0,0 +1,11 @@ +from django import forms +from .models import NoticeBoard + +class PostForm(forms.ModelForm): + class Meta: + model = NoticeBoard + fields = ['title', 'contents'] + widgets = { + 'title': forms.TextInput(attrs={'class': 'form-control'}), + 'contents': forms.Textarea(attrs={'class': 'form-control', 'id': 'markdown-editor'}), + } diff --git a/butler/templates/butler/create_notice.html b/butler/templates/butler/create_notice.html new file mode 100644 index 0000000..a6d4bde --- /dev/null +++ b/butler/templates/butler/create_notice.html @@ -0,0 +1,50 @@ +{% extends "components/base.html" %} +{% block title %}Create Notice{% endblock %} + +{% block main_area %} +

Create New Notice

+
+
+
+
+ {% csrf_token %} +
+ {{ form.title.label_tag }} + {{ form.title }} +
+ + +

contents

+ +
+ {{ form.contents }} +
+ +
+ + Cancel +
+
+
+
+
+
+
+ + + + + {% endblock %} diff --git a/butler/templates/butler/notice_detail.html b/butler/templates/butler/notice_detail.html index 97a2441..c6729bb 100644 --- a/butler/templates/butler/notice_detail.html +++ b/butler/templates/butler/notice_detail.html @@ -15,5 +15,5 @@ {{ notice.created_at|date:"Y-m-d H:i" }}

수정일: {{ notice.updated_at|date:"Y-m-d H:i" }}

- Back to Notices + Back to Notices {% endblock %} diff --git a/butler/templates/butler/notice.html b/butler/templates/butler/notice_list.html similarity index 72% rename from butler/templates/butler/notice.html rename to butler/templates/butler/notice_list.html index 10334da..e48e3aa 100644 --- a/butler/templates/butler/notice.html +++ b/butler/templates/butler/notice_list.html @@ -3,7 +3,10 @@ {% block title %}Notice{% endblock %} {% block main_area %} -

공지사항

+

+ 공지사항 + +

{% if records %} @@ -32,4 +35,10 @@ {% else %}

현재 공지사항이 없습니다.

{% endif %} + {% if request.user.is_authenticated %} + + + {% endif %} {% endblock %} diff --git a/butler/urls.py b/butler/urls.py index 675890a..fa24850 100644 --- a/butler/urls.py +++ b/butler/urls.py @@ -5,7 +5,8 @@ app_name = 'butler' urlpatterns = [ path('', views.hello_view, name='landing'), # 루트 경로에서 hello_view 호출 - path('notice', views.notice_view, name='notice'), + path('notice', views.notice_list, name='notice_list'), + path('create_notice/', views.create_notice, name='create_notice'), # 포스트 작성 path('notice//', views.notice_detail_view, name='notice_detail'), path('ip_mgmt', views.ip_mgmt_view, name='ip_mgmt'), path('ip-mgmt/add/', views.add_ip_record, name='ip_mgmt_add'), # 데이터 추가 경로 diff --git a/butler/views.py b/butler/views.py index 704de53..20d8ce5 100644 --- a/butler/views.py +++ b/butler/views.py @@ -1,8 +1,10 @@ from django.shortcuts import render, redirect, get_object_or_404 +from django.contrib.auth.decorators import login_required from pathlib import Path import markdown import os from .models import NoticeBoard, IPManagementRecord +from .forms import PostForm from django.db.models import Q @@ -12,11 +14,24 @@ def hello_view(request): "butler/landing.html", ) - # --- notice --- -def notice_view(request): +@login_required +def create_notice(request): + if request.method == 'POST': + form = PostForm(request.POST) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user # 작성자 정보 추가 + post.save() + form.save_m2m() + return redirect('butler:notice_list') + else: + form = PostForm() + return render(request, 'butler/create_notice.html', {'form': form}) + +def notice_list(request): records = NoticeBoard.objects.all() - return render(request, "butler/notice.html", {"records": records}) + return render(request, "butler/notice_list.html", {"records": records}) def notice_detail_view(request, pk):