반응형
CodingEntrepreneurs Django 강의 정리
object 처리 순서>
queryset -> list of python object
title__icontains>
필터링 적용
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):
print("DJANGO SAYS", request.method, request.path, request.user)
# obj = BlogPost.objects.get(slug=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)
# CRUD
# GET -> Retrieve / List
# POST -> Create / Update / DELETE
# Create Retrieve Update Delete
def blog_post_list_view(request):
# list out objects
# could be search
qs = BlogPost.objects.all()
# qs = BlogPost.objects.filter(title__icontains="hello")
template_name = "blot_post_list.html"
context = {"object_list": qs}
return render(request, template_name, context)
def blog_post_create_view(request):
# create objects
# ? use a form
template_name = "blot_post_create.html"
context = {"form": None}
return render(request, template_name, context)
def blog_post_detail_view(request, slug):
obj = get_object_or_404(BlogPost, slug=slug)
template_name = "blog_post_detail.html"
context = {"object": obj}
return render(request, template_name, context)
def blog_post_update_view(request):
obj = get_object_or_404(BlogPost, slug=slug)
template_name = "blog_post_update.html"
context = {"object": obj, "form": None}
return render(request, template_name, context)
def blog_post_delete_view(request):
obj = get_object_or_404(BlogPost, slug=slug)
template_name = "blog_post_delete.html"
context = {"object": obj}
return render(request, template_name, context)
blot_post_list.html>
{% extends "base.html" %}
{% block content %}
{% for object in object_list %}
<li>{{ object.title }}</li>
{% endfor %}
{% endblock %}
urls>
"""try_django URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path # url
from blog.views import (
blog_post_detail_page,
blog_post_list_view,
)
from .views import (
home_page,
about_page,
contact_page,
example_page,
)
urlpatterns = [
path("", home_page),
path("blog/", blog_post_list_view),
path("blog/<str:slug>/", blog_post_detail_page),
# re_path(r"^blog/(?P<slug>\w+)/$", blog_post_detail_page),
path("page", about_page),
path("pages", about_page),
re_path(r"^pages?/$", about_page),
re_path(r"^about/$", about_page),
path("contact/", contact_page),
path("example/", example_page),
path("admin/", admin.site.urls),
]
적용 스크린샷>
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]Include URLs (0) | 2020.06.17 |
---|---|
[Django]Routing the Views (0) | 2020.06.16 |
[Django]CRUD View Outline (0) | 2020.06.16 |
[Django]CRUD & Views (0) | 2020.06.16 |
[Django]A Unique Slug (0) | 2020.06.16 |