반응형
CodingEntrepreneurs Django 강의 정리
templates>
create.html>
{% extends "base.html" %}
{% block content %}
<h1>Create new blog post</h1>
{% endblock %}
delete.html>
{% extends "base.html" %}
{% block content %}
<h1>{{ object.title }} - Delete</h1>
<p>{{ object.content }}</p>
{% endblock %}
detail.html>
{% extends "base.html" %}
{% block content %}
<h1>{{ object.title }}</h1>
<p>{{ object.content }}</p>
{% endblock %}
list.html>
{% extends "base.html" %}
{% block content %}
{% for object in object_list %}
<li>{{ object.title }}</li>
{% endfor %}
{% endblock %}
update.html>
{% extends "base.html" %}
{% block content %}
<h1>{{ object.title }} - Update</h1>
<p>{{ object.content }}</p>
{% endblock %}
try_django/urls>
"""try_django URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/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, include # url
from blog.views import blog_post_create_view
from .views import home_page, about_page, contact_page, example_page
urlpatterns = [
path("", home_page),
path("blog-new/", blog_post_create_view),
path("blog/", include("blog.urls")),
# re_path(r'^blog/(?P<slug>\w+)/$', blog_post_detail_view),
path("page/", about_page),
path("pages/", about_page),
re_path(r"^pages?/$", about_page),
re_path(r"^about/$", about_page),
path("example/", example_page),
path("contact/", contact_page),
path("admin/", admin.site.urls),
]
blog/urls>
from django.urls import path
from .views import (
blog_post_detail_view,
blog_post_list_view,
blog_post_update_view,
blog_post_delete_view,
)
urlpatterns = [
path("", blog_post_list_view),
path("<str:slug>/", blog_post_detail_view),
path("<str:slug>/edit/", blog_post_update_view),
path("<str:slug>/delete/", blog_post_delete_view),
]
views>
from django.http import Http404
from django.shortcuts import render, get_object_or_404
# Create your views here.
from .models import BlogPost
# 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() # queryset -> list of python object
template_name = "blog/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 = "blog/create.html"
context = {"form": None}
return render(request, template_name, context)
def blog_post_detail_view(request, slug):
# 1 object -> detail view
obj = get_object_or_404(BlogPost, slug=slug)
template_name = "blog/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/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/delete.html"
context = {"object": obj}
return render(request, template_name, context)
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]A Django Form (0) | 2020.06.17 |
---|---|
[Django]Submit Raw HTML Form (0) | 2020.06.17 |
[Django]Include URLs (0) | 2020.06.17 |
[Django]Routing the Views (0) | 2020.06.16 |
[Django]Blog Post List View (0) | 2020.06.16 |