반응형
unique>
고유한 필드
models>
from django.db import models
# Create your models here.
class BlogPost(models.Model):
# id = models.IntegerField() # pk
title = models.TextField()
slug = models.SlugField(unique=True) # hello world > hello-world
content = models.TextField(null=True, blank=True)
views>
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from .models import BlogPost
# Create your views here.
# GET > 1 object : 1개 오브젝트 수용
# filter > [] object : 리스트 형태 오브젝트 수용
def blog_post_detail_page(request, slug):
# queryset = BlogPost.objects.filter(slug=slug)
# if queryset.count() == 0:
# raise Http404
# obj = queryset.first()
# print(post_id.__class__)
obj = get_object_or_404(BlogPost, slug=slug)
template_name = "blog_post_detail.html"
context = {"object": obj} # {'title': objecct.title}
return render(request, template_name, context)
적용 스크린샷>
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]CRUD View Outline (0) | 2020.06.16 |
---|---|
[Django]CRUD & Views (0) | 2020.06.16 |
[Django]QuerySet Lookups (0) | 2020.06.16 |
[Django]A New Database Lookup Value (0) | 2020.06.16 |
[Django]get_object_or_404 (0) | 2020.06.15 |