티스토리 뷰
python/django_ERP2
(macOS)[python] django-erp : Model - Notice 생성/수정/삭제
jinozpersona 2021. 12. 7. 10:51INTRO
Python 3.10.0
Django 3.2.8
적용 후 미리보기
- localhost:8000/home/notice
공지사항 옆 글쓰기 클릭 : localhost:8000/home/notice_new
2번 공지사항 제목 클릭 : localhost:8000/home/notice_view/2
하단 수정 클릭 : localhost:8000/home/notice_view/2/edit
--the end--
-----python coding-----------------------------------------------------------------------------------
Model 생성하기 : Notice(공지사항)
- home/models.py
from django.db import models
from django.conf import settings
class Notice(models.Model):
notice_id = models.AutoField(primary_key=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False)
title = models.CharField('Title', max_length=128, null=False)
article = models.TextField('Article', null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
hit = models.PositiveIntegerField(default=0)
def __str__(self):
return self.title
@property
def hit_cnt(self):
self.hit = self.hit + 1
self.save()
- models.py DB 반영
$ ./manage.py makemigrations home
$ ./manage.py migrate
Form 만들기
- home/forms.py 생성
from django import forms
from django.contrib.auth.forms import UserCreationForm
from home.models import Notice
class NoticeForm(forms.ModelForm):
class Meta:
model = Notice
fields = ['author', 'title', 'article']
widgets = {
'title' : forms.TextInput(attrs={'class': 'form-control', 'style':'width: 30em'}),
'article' : forms.Textarea(attrs={'class': 'form-control', 'style':'width: 80em', 'rows':10}),
}
labels = {
'author' : '이름',
'title' : '제목',
'article' : '내용',
}
urls : notice, notice_new, notice_view, notice_edit, notice_delete
- home/urls.py
from django.contrib import admin
from django.urls import path
from django.contrib.auth import views as auth_views
from django.views.generic import TemplateView
from home import views
app_name = 'home'
urlpatterns = [
path('', views.index, name='index'),
path('notice/', views.notice, name='notice'),
path('notice_view/<int:pk>', views.notice_view, name='notice_view'),
path('notice_new/', views.notice_new, name='notice_new'),
path('notice_view/<int:pk>/edit', views.notice_edit, name='notice_edit'),
path('notice_view/<int:pk>/delete', views.notice_delete, name='notice_delete'),
]
views : notice, notice_new, notice_view, notice_edit, notice_delete
- home/views.py
from django.shortcuts import render, redirect
from django.contrib import auth
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
from home.models import Notice
from home.forms import NoticeForm
# Create your views here.
@login_required
def index(request):
return render(request, 'persona/index.html')
@login_required
def notice(request):
notices = Notice.objects.all().order_by('-notice_id')
context = {'notices':notices}
return render(request, 'persona/notices/notice.html', context)
@login_required
def notice_view(request,pk):
notices = Notice.objects.all().order_by('-notice_id')
notice_view = Notice.objects.get(pk=pk)
context = {'notices':notices, 'notice_view':notice_view}
return render(request, 'persona/notices/notice_view.html', context)
@login_required
def notice_new(request):
notices = Notice.objects.all().order_by('-notice_id')
if request.method == 'POST':
form = NoticeForm(request.POST)
if form.is_valid():
notice_new = Notice()
notice_new.author = form.cleaned_data['author']
notice_new.title = form.cleaned_data['title']
notice_new.article = form.cleaned_data['article']
notice_new.save()
return render(request, 'persona/notices/notice.html',{'notices':notices})
else:
form = NoticeForm()
context = {'form':form, 'notices':notices}
return render(request, 'persona/notices/notice_new.html', context)
@login_required
def notice_delete(request, pk):
notice_del = Notice.objects.get(pk=pk)
if notice_del.author == User.objects.get(username = request.user.get_username()):
notice_del.delete()
notices = Notice.objects.all().order_by('-notice_id')
return render(request, 'persona/notices/notice.html',{'notices':notices})
else:
return render(request,'persona/errorforms/401.html')
@login_required
def notice_edit(request,pk):
notices = Notice.objects.all().order_by('-notice_id')
notice_edit = Notice.objects.get(pk=pk)
if request.method == 'POST':
form = NoticeForm(request.POST)
if form.is_valid():
notice_edit.author = form.cleaned_data['author']
notice_edit.title = form.cleaned_data['title']
notice_edit.article = form.cleaned_data['article']
notice_edit.save()
return render(request, 'persona/notices/notice.html',{'notices':notices})
else:
form = NoticeForm(instance=notice_edit)
notices = Notice.objects.all().order_by('-notice_id')
context = {'form':form, 'notices':notices}
return render(request, 'persona/notices/notice_edit.html', context)
templates : notice.html, notice_view.html, notice_new.html, notice_edit.html
- home/templates/persona/notices 폴더생성
- home/templates/persona/notices/notice.html
{% extends '../base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
<!-- Main_Header -->
<h1 class="mt-4">공지사항</h1>
<ol class="breadcrumb mb-4">
<li class="breadcrumb-item"><a href="{% url 'home:index' %}">Dashboard</a></li>
<li class="breadcrumb-item active">Notice</li>
</ol>
<!-- Main_content_#1 -->
<!-- Main_content_#2 -->
<!-- Main_content_#3 -->
<div class="card mb-4">
<div class="card-header">
<i class="fas fa-table me-1"></i>
공지사항 | <a href="{% url 'home:notice_new' %}">글쓰기</a>
</div>
<div class="card-body">
<table id="datatablesSimple">
<thead>
<tr>
<th>순번</th>
<th>작성자</th>
<th>제목</th>
<th>발행일</th>
<th>수정일</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
{% for notice in notices %}
<tr>
<td>{{ notice.notice_id }}</td>
<td>{{ notice.author }}</td>
<td><a href="{% url 'home:notice_view' notice.pk %}">{{ notice.title }}</a></td>
<td>{{ notice.created_at }}</td>
<td>{{ notice.updated_at }}</td>
<td>{{ notice.hit }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<!-- Main_content_#4 -->
</div>
{% endblock main %}
{% load static %}
{% block js %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="{% static 'js/scripts.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
<script src="{% static 'assets/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-bar-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-pie-demo.js' %}"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
<script src="{% static 'js/datatables-simple-demo.js' %}"></script>
{% endblock js %}
- home/templates/persona/notices/notice_view.html
{% extends '../base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
<!-- Main_Header -->
<h1 class="mt-4">공지사항</h1>
<ol class="breadcrumb mb-4">
<li class="breadcrumb-item"><a href="{% url 'home:index' %}">Dashboard</a></li>
<li class="breadcrumb-item active">Notice</li>
</ol>
<!-- Main_content_#1 -->
<!-- Main_content_#2 -->
<!-- Main_content_#3 -->
<div class="card mb-4">
<div class="card-header">
<i class="fas fa-table me-1"></i>
공지사항 | <a href="{% url 'home:notice_new' %}">글쓰기</a>
</div>
<div class="card-body">
<table id="datatablesSimple">
<thead>
<tr>
<th>순번</th>
<th>작성자</th>
<th>제목</th>
<th>발행일</th>
<th>수정일</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
{% for notice in notices %}
<tr>
<td>{{ notice.notice_id }}</td>
<td>{{ notice.author }}</td>
<td><a href="{% url 'home:notice_view' notice.pk %}">{{ notice.title }}</a></td>
<td>{{ notice.created_at }}</td>
<td>{{ notice.updated_at }}</td>
<td>{{ notice.hit }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<!-- Main_content_#4 -->
<div class="card mb-4">
<div class="card-header">
<span style="font-size:1.3em">{{ notice_view.title }}</span><br>
<span style="font-size:0.9em"><a href="{% url 'home:notice_edit' notice_view.pk %}">수정</a> | <a href="{% url 'home:notice_delete' notice_view.pk %}">삭제</a></span>
</ol>
</div>
<div class="card-body">
<p style="display:none">{{ notice_view.hit_cnt }}</p>
{{ notice_view.article }}
</div>
</div>
</div>
{% endblock main %}
{% load static %}
{% block js %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="{% static 'js/scripts.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
<script src="{% static 'assets/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-bar-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-pie-demo.js' %}"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
<script src="{% static 'js/datatables-simple-demo.js' %}"></script>
{% endblock js %}
- home/templates/persona/notices/notice_new.html
{% extends '../base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
<!-- Main_Header -->
<h1 class="mt-4">공지사항</h1>
<ol class="breadcrumb mb-4">
<li class="breadcrumb-item"><a href="{% url 'home:index' %}">Dashboard</a></li>
<li class="breadcrumb-item active">Notice</li>
</ol>
<!-- Main_content_#1 -->
<!-- Main_content_#2 -->
<!-- Main_content_#3 -->
<div class="card mb-4">
<div class="card-header">
<i class="fas fa-table me-1"></i>
공지사항 | <a href="{% url 'home:notice_new' %}">글쓰기</a>
</div>
<div class="card-body">
<table id="datatablesSimple">
<thead>
<tr>
<th>순번</th>
<th>작성자</th>
<th>제목</th>
<th>발행일</th>
<th>수정일</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
{% for notice in notices %}
<tr>
<td>{{ notice.notice_id }}</td>
<td>{{ notice.author }}</td>
<td><a href="{% url 'home:notice_view' notice.pk %}">{{ notice.title }}</a></td>
<td>{{ notice.created_at }}</td>
<td>{{ notice.updated_at }}</td>
<td>{{ notice.hit }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<!-- Main_content_#4 -->
<div class="card mb-4">
<div class="card-header">
<span style="font-size:1.3em">새글 작성하기</span>
</div>
<div class="card-body">
<p style="display:none">{{ notice_view.hit_cnt }}</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<a href="{% url 'home:notice' %}"><button type="submit">생성</button></a>
</form>
</div>
</div>
</div>
{% endblock main %}
{% load static %}
{% block js %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="{% static 'js/scripts.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
<script src="{% static 'assets/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-bar-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-pie-demo.js' %}"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
<script src="{% static 'js/datatables-simple-demo.js' %}"></script>
{% endblock js %}
- home/templates/persona/notices/notice_edit.html
{% extends '../base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
<!-- Main_Header -->
<h1 class="mt-4">공지사항</h1>
<ol class="breadcrumb mb-4">
<li class="breadcrumb-item"><a href="{% url 'home:index' %}">Dashboard</a></li>
<li class="breadcrumb-item active">Notice</li>
</ol>
<!-- Main_content_#1 -->
<!-- Main_content_#2 -->
<!-- Main_content_#3 -->
<div class="card mb-4">
<div class="card-header">
<i class="fas fa-table me-1"></i>
공지사항 | <a href="{% url 'home:notice_new' %}">글쓰기</a>
</div>
<div class="card-body">
<table id="datatablesSimple">
<thead>
<tr>
<th>순번</th>
<th>작성자</th>
<th>제목</th>
<th>발행일</th>
<th>수정일</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
{% for notice in notices %}
<tr>
<td>{{ notice.notice_id }}</td>
<td>{{ notice.author }}</td>
<td><a href="{% url 'home:notice_view' notice.pk %}">{{ notice.title }}</a></td>
<td>{{ notice.created_at }}</td>
<td>{{ notice.updated_at }}</td>
<td>{{ notice.hit }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<!-- Main_content_#4 -->
<div class="card mb-4">
<div class="card-header">
<span style="font-size:1.3em">수정하기</span>
</div>
<div class="card-body">
<p style="display:none">{{ notice_view.hit_cnt }}</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<a href="{% url 'home:notice' %}"><button type="submit">수정</button></a>
</form>
</div>
</div>
</div>
{% endblock main %}
{% load static %}
{% block js %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="{% static 'js/scripts.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
<script src="{% static 'assets/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-bar-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-pie-demo.js' %}"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
<script src="{% static 'js/datatables-simple-demo.js' %}"></script>
{% endblock js %}
반응형
'python > django_ERP2' 카테고리의 다른 글
(macOS)[python] django-erp : templates 구조 정리 - 2 (0) | 2021.12.08 |
---|---|
(macOS)[python] django-erp : templates 구조 정리 - 1 (0) | 2021.12.08 |
(macOS)[python] django-erp : (3) persona-erp bootstrap customize (0) | 2021.11.15 |
(macOS)[python] django-erp : templates 분리 (2) bstest layout 분리 (0) | 2021.11.15 |
(macOS)[python] django-erp : templates 분리 (1) mysite (0) | 2021.11.07 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 확진
- Model
- Django
- Templates
- arduino
- raspberrypi
- Raspberry Pi
- 코로나19
- DAQ
- CSV
- github
- Python
- 자가격리
- sublime text
- Regression
- COVID-19
- git
- SSH
- pyserial
- DS18B20
- ERP
- 라즈베리파이
- vscode
- r
- template
- 코로나
- Pandas
- analysis
- server
- MacOS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함