본문 바로가기

Python_WEB/Try_Django

[Django]Blog Post Navigation

728x90

CodingEntrepreneurs Django 강의 정리

models>

from django.conf import settings
from django.db import models

# Create your models here.

User = settings.AUTH_USER_MODEL


class BlogPost(models.Model):  # blogpost_set > queryset
    # id = models.IntegerField()    # pk
    user = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)  # hello world > hello-world
    content = models.TextField(null=True, blank=True)

    def get_absolute_url(self):
        return f"/blog/{self.slug}"

    def get_edit_url(self):
        return f"{self.get_absolute_url}/edit"

    def get_delete_url(self):
        return f"{self.get_absolute_url}/delete"

 

detail.html>

{% extends "base.html" %}

{% block content %}

<h1>{{ object.title }}</h1>
<p>{{ object.content }}</p>

{% if request.user.is_authenticated %}
{% if request.user.is_staff %}
<a href="{{ object.get_edit_url }}">Edit</a>
<a href="{{ object.get_delete_url }}">Delete</a>
{% endif %}
{% endif %}

{% endblock %}

 

list.html>

{% extends "base.html" %}

{% block content %}

{% for object in object_list %}
<li><a href="{{ object.get_absolute_url }}">{{ object.title }}</a></li>
{% endfor %}

{% endblock %}
반응형

'Python_WEB > Try_Django' 카테고리의 다른 글

[Django]Include with Arguments  (0) 2020.06.21
[Django]Include the Navbar  (0) 2020.06.21
[Django]Blog Post Navigation  (0) 2020.06.21
[Django]Delete and Confirm  (0) 2020.06.21
[Django]Better Validation on Update Views  (0) 2020.06.21
[Django]Update View with Model Form  (0) 2020.06.21