Articleapp models 오류 수정
auto_create > auto_now_add = true
Articleapp models
class Aticle(models.Model):
writer = models.ForignKey(User, on_delete=models.SET_NULL, related_name='article')
title = models.CharField(max_length=200, null=True)
image = models.ImageField(upload_to='article/', null=False)
content = models.TextField(null=Ture)
created_at = models.DateField(auto_now_add=True)
Python
복사
model form도 만들어준다
class AricleCreationForm(ModelForm):
class Meta:
model = Article
fields = ['title', 'image', 'content']
Python
복사
Articleapp View
@method_decorator(login_required, 'get')
@method_decorator(login_required, 'post')
class ArticleCreateView(CreateView):
model = Article
form_class = ArticleCreationForm
template_name = 'articleapp/create.view'
def form_valid(self,form):
temp_article = form.save(commit=False)
temp_article.writer = self.request.user
temp_article.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('articleapp/detail.html', kwargs={'pk':self.object.pk})
class ArticleDetailView(DetailView):
model = Article
context_object_name = 'target_article'
template_name = 'articleapp/detial.html'
Python
복사
detail.html, create.html을 만들어준다
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style = "text-align: center; max-width: 600px; margin: 4rem auto">
<div class="mb-4">
<h4>Article Create</h4>
</div>
<form action="{% url 'articleapp:create' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
Python
복사
create.html
{% extends 'base.html' %}
{% block content %}
<div>
<div style="text-align: center; max-width: 500px; margin: 4rem auto;">
<h1>
{{ target_article.title }}
</h1>
<img src="{{ target_article.image.url }}" alt="">
<p>
{{ target_article.content }}
</p>
</div>
</div>
{% endblock %}
Python
복사
detail.html
urls.py에 라우팅도 해준다.
urlpatterns = [
path('create/', ArrticleCreateView.as_view(), name='create'),
path('detail/<int:pk>', ArtricleDetailView.as_view(), name='detail),
]
Python
복사
그 후, header에 네비게이션에서 list로 연결될 수 있게 해준다.
<a href="{% url 'articleapp:list' %}">
Create Article
</a>
Python
복사
UpdateView
decorator를 복사해서 키를 수정해 준 후,
@method_decorator(article_ownership_required, 'get')
@method_decorator(article_ownership_required, 'post')
class ArticleUpdateView(UpdateView):
model = Article
form_class = ArticleCreationForm
context_object_name = 'target_article
template_name = 'articleapp/update.html'
def get_success_url(self):
return reverse('articleapp:detail', kwargs={'pk':object.pk})
Python
복사
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style = "text-align: center; max-width: 600px; margin: 4rem auto">
<div class="mb-4">
<h4>Update Article</h4>
</div>
<form action="{% url 'articleapp:update' pk=target_article.pk %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
Python
복사
detail view에서 update view로 이동하는 앵커태그도 걸어준다
<a href="{% url 'articleapp:update' pk=target_article.pk %]">
<p>Update Article</p>
</a>
HTML
복사
Delete View
@method_decorator(login_required, 'get')
@method_decorator(login_required, 'post')
class ArticleDeleteView(DeleteView):
model = Article
context_object_name = 'target_article'
template_name = 'articleapp/delete.html'
success_url = reverse_lazy('articleapp:list')
Python
복사
html도 accountapp에서 가져온다
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style = "text-align: center; max-width: 600px; margin: 4rem auto">
<div class="mb-4">
<h4>Delete Article : {{ target_article.title }}</h4>
</div>
<form action="{% url 'articleapp:delete' pk=target_article.pk%}" method="post">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-danger rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
Python
복사
{{ target_article.title }} 로 지울 게시글의 이름을 표기해주고, form 태그 내 url을 articleapp으로 바꿔준다
detail에서 delete로 넘어갈 수 있는 앵커태그를 걸어준다.
<a href="{%url 'articleapp:delete' pk=target_article.pk %}" class="btn btn-danger rounded-pill col-3">
<p>Delete Article</p>
</a>
Python
복사