티스토리 뷰

728x90

 

++ 장고 공식 문서는 항상 확인하기 

https://docs.djangoproject.com/en/4.2/

 

 

 

1.  작성한 글을 쓰고 나면 바로 게시판으로 갈 수 있게 바꾸자.

 

  • views.py 를 변형시켜준다.
  • 그냥 html 만 바꾸면 에러가 난다.
from django.shortcuts import render, redirect

...

def create(request):
  title = request.POST.get("title")
  content = request.POST.get("content")
  article = Article(title=title, content=content)
  article.save()
  return redirect("articles")

 

 

 

 

 

 

  • 필요없는 create.html 은 지워준다.!

 

 

 

 

 

 

 

 

2.  작성한 글을 쓰고 나면 그 상세페이지가 보일 수 있도록 하자.

 

  •  articles 에 urls.py 에서 path를 추가해준다.- 어떤 경로가 아니라 바로 id값으로 가기 때문에 바로 써준다.
path("<int:pk>/", views.detail, name="detail"),

 

  • views.py 를 변형시켜준다.
def detail(request, pk):
  article = Article.objects.get(pk=pk)
  context = {
    "article": article,
    }
  return render(request, "detail.html", context)

 

  • detail.html 파일을 만들어준다.
{% extends 'base.html' %} 

{% block content %}
  <h2>글 상세 페이지</h2>
  <p>제목: {{ article.title }}</p>
  <p>내용: {{ article.content }}</p>
  <p>작성일시: {{ article.created_at }}</p>
  <p>수정일시: {{ article.updated_at }}</p>

  <a href="{% url 'articles' %}">목록 보기</a>

{% endblock content %}

 

 

 

 

++ 주소는 http://127.0.0.1:8000/articles/8/ 로 적어야 8번째 글로 이동함.

 

 

 

  • 글을 쓰고 나서 바로 상세페이지로 가기 위해서 detail 만 바꾸어서는 안되고 키값을 주어야한다.
return redirect("detail", article.id)

 

 

 

 

 

 

 

 

 

 

3.  편의성 업데이트

  • articles.html 을 바꿔주자. 바로 새로운 글을 작성하고, 글을 클릭하면 그 글의 상세페이지로 이동할 수 있게.
{% extends 'base.html' %} 

{% block content %}
  <h1> articles </h1>
  <a href="{% url 'new' %}">
    <button> 새로운 글 작성</button>
  </a>
  <ul>
    {% for article in articles %}
      <li>
        <a href="{% url 'detail' article.pk %}">
        <p>글 번호 : {{ article.id }}</p>
        <p>글 제목 : {{ article.title }}</p>
        </a>
        <hr>
      </li>
    {% endfor %}
  </ul>

{% endblock content %}

 

 

  • new.html 에도 목록으로 가는 칸을 추가해준다.
<a href="{% url 'articles' %}">목록 으로</a>

 

 

 

 

 

 

 

 

 

 

 

4.  글 삭제하기

  • articles 에 urls.py 에서 path를 추가해준다
path("<int:pk>/delete/", views.delete, name="delete"),

 

  • views.py 를 변형시켜준다.
def delete(request, pk):
  article = Article.objects.get(pk=pk)
  article.delete()
  return redirect("articles")

 

  • detail.html 파일에서 수정해준다.
{% extends "base.html" %}

{% block content %}

    <h2>글 상세 페이지</h2>
    <p>제목: {{ article.title }}</p>
    <p>내용: {{ article.content }}</p>
    <p>작성일시: {{ article.created_at }}</p>
    <p>수정일시: {{ article.updated_at }}</p>

    <a href="{% url 'articles' %}"><button>목록 보기</button></a>

    <hr>
    <form action="{% url 'delete' article.pk %}" method="POST">
        {% csrf_token %}
        <input type="submit" value="글삭제">
    </form>

{% endblock content %}

 

 

 

  • 여기서 get으로 가면 글이 없어진다! http://127.0.0.1:8000/articles/9/delete 로 바로 가면 지워짐!
  • post 방식으로 바꾸어주어야함.
def delete(request, pk):
  article = Article.objects.get(pk=pk)
  if request.method == "POST":
      article.delete()
      return redirect("articles")
  return redirect("detail", article.pk)

 

 

 

 

 

 

 

 

 

5.  글 수정하기

  • articles 에 urls.py 에서 path를 추가해준다
path("<int:pk>/edit", views.edit, name="edit"),
path("<int:pk>/update/", views.update, name="update"),

 

  • views.py 를 변형시켜준다.
...
def edit(request, pk):
	article = Article.objects.get(pk=pk)
	context = {
	    "article": article,
	}
	return render(request, "edit.html", context)
    
def edit(request, pk):
  title = request.POST.get("title")
  content = request.POST.get("content")

	article = Article.objects.get(pk=pk)
	article.title = title
  article.content = content
  article.save()

	return redirect("detail", article.id)
...

 

  • edit.html 파일을 만들어 준다..
{% extends 'base.html' %} 

{% block content %}
  <h1>Edit Article</h1>

  <form action="{% url 'update' article.pk %}" method="POST">
    {% csrf_token %}
    <label for="title">제목</label>
    <input type="text" name="title" id="title" value="{{ article.title }}">
    <br><br>

    <label for="content">내용</label>
    <textarea name="content" id="content" cols="30" rows="10">{{article.content}}</textarea><br><br>

    <button type="submit">수정</button>
  </form>

  <a href="{% url 'detail' article.pk %}">이전으로</a>

{% endblock content %}

 

 

  • detail.html 파일에서 수정해준다. 수정하기 버튼 만들기
{% extends 'base.html' %} 

{% block content %}
  <h2>글 상세 페이지</h2>
  <p>제목: {{ article.title }}</p>
  <p>내용: {{ article.content }}</p>
  <p>작성일시: {{ article.created_at }}</p>
  <p>수정일시: {{ article.updated_at }}</p>

  <a href="{% url 'articles' %}">목록 보기</a>
  <hr>
  <a href="{% url 'edit' article.pk %}"><button>글수정</button></a>
  
  <form action="{% url 'delete' article.pk %}" method="POST">
      {% csrf_token %}
      <input type="submit" value="글삭제">
  </form>


{% endblock content %}

 

 

 

 

 

 

 

 

반응형
반응형
TAG
more
최근에 올라온 글