- project, app 생성
django-admin startproject [projectname] django-admin startapp app [appname]
- runserver
python manage.py runserver
include('주소')
를 통해 app으로 위임할 수 있다# project/urls.py from django.urls import path, include urlpatterns = [ path('',include('myapp.urls')) ]
views로 위임하는 것은 include 없이 가능하다.
# myapp/urls.py from django.urls import path from myapp import views urlpatterns = [ path('<id>',views.index), ]
view 안의 함수는 기본적으로 request를 인자로 가진다. 추가적인 입력을 가지기 위해선 urls.py의 주소에
<id>
와 같은 형태로 표현 해주어야 함#views.py from django.shortcuts import render, HttpResponse def index(request,id): return HttpResponse('Hello!'+id+'!')
# app/view.py
from types import NoneType
from django.shortcuts import render, redirect, HttpResponse
import random
from django.views.decorators.csrf import csrf_exempt
nextId = 4
topics = [
{'id': 1, 'title': 'Routing', 'body': 'Routing is ...'},
{'id': 2, 'title': 'View', 'body': 'View is ...'},
{'id': 3, 'title': 'Model', 'body': "Model is ..."}
]
def HTMLTemplate(articleTag, id=None):
global topics
ol = ''
delete = ''
update = ''
if id:
delete = f'''
<li>
<form action="/delete/" method="post">
<input type="hidden" name="id" value={id}>
<input type="submit" value="delete">
</form>
</li>
'''
update = f'<li><a href="/update/{id}/">update</a></li>'
for topic in topics:
ol += f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
return f'''
<html>
<body>
<h1><a href="/">Django</a></h1>
<ol>
{ol}
</ol>
<p>
{articleTag}
</p>
<ul>
<li><a href="/create/">create</a></li>
{delete}
{update}
</ul>
</body>
</html>
'''
def index(request):
article = '''
<h2>Welcome</h2>
Hello, Django
'''
return HttpResponse(HTMLTemplate(article))
@csrf_exempt
def create(request):
global topics
global nextId
if request.method == 'GET':
article = '''
<form action="/create/" method="post">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea name="body" placeholder="body"></textarea></p>
<p><input type="submit"></p>
</form>
'''
return HttpResponse(HTMLTemplate(article))
elif request.method == 'POST':
title = request.POST['title']
body = request.POST['body']
newtopic = {'id': nextId, 'title': title, 'body': body}
topics.append(newtopic)
url = f'/read/{nextId}'
nextId += 1
return redirect(url)
@csrf_exempt
def update(request, id):
global topics
if request.method == 'GET':
for topic in topics:
if topic["id"] == int(id):
title = topic["title"]
body = topic["body"]
article = f'''
<form action="/update/{id}/" method="post">
<p><input type="text" name="title" placeholder="title" value={title}></p>
<p><textarea name="body" placeholder="body">{body}</textarea></p>
<p><input type="submit"></p>
</form>
'''
return HttpResponse(HTMLTemplate(article))
elif request.method == 'POST':
title = request.POST['title']
body = request.POST['body']
for topic in topics:
if topic["id"] == int(id):
topic["title"] = title
topic["body"] = body
url = f'/read/{id}'
return redirect(url)
@csrf_exempt
def delete(request):
global topics
if request.method == "POST":
newtopics = []
id = request.POST["id"]
for topic in topics:
if topic["id"] != int(id):
newtopics.append(topic)
topics = newtopics
return redirect('/')
def read(request, id):
global topics
article = ''
for topic in topics:
if topic['id'] == int(id):
article += f'<h2>{topic["title"]}</h2><p>{topic["body"]}</p>'
return HttpResponse(HTMLTemplate(article, id))
#app/urls.py
from django.urls import path
from myapp import views
urlpatterns = [
path('', views.index),
path('create/', views.create),
path('read/<id>/', views.read),
path('delete/', views.delete),
path('update/<id>/', views.update),
]