Django runserver 이용하여 실행하기
//윈도우
python manage.py runserver
python manage.py runserver //8888(port)
//Mac
python3 manage.py runserver
myproject > urls.py
"""myproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.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,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
myapp > urls.py 라우터
from django.urls import path
from myapp import views
urlpatterns = [
path('', views.index),
path('create/',views.create),
path('read/<id>/',views.read), #read 함수의 파라미터에 id값 전해줌
path('delete/', views.delete),
path('update/<id>/',views.update),
]
myapp > views.py
from django.http import HttpResponse
from django.shortcuts import render,redirect
import random
from django.views.decorators.csrf import csrf_exempt
def HTMLTemplate(articleTag, id=None):
global topics
contextUI = ''
if id!=None:
contextUI = f'''
<li>
<form action="/delete/" method="post">
<input type="hidden" name="id" value={id}>
<input type="submit" value="delete">
</form>
</li>
<li><a href="/update/{id}">update</a></li>
'''
ol = ''
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>
{articleTag}
<ul>
<li><a href="/create/">create</a></li>
{contextUI}
</ul>
</body>
</html>
'''
nextId = 4
topics = [
{'id':1, 'title':'routing', 'body':'Routing is ..'},
{'id':2, 'title':'view', 'body':'View is ..'},
{'id':3, 'title':'Model', 'body':'Model is ..'}
]
# Create your views here.
def index(request):
article = '''
<h2>Welcome</h2>
Hello, Django
'''
return HttpResponse(HTMLTemplate(article))
def read(request,id):
global topics
article = ''
for topic in topics:
if topic['id'] == int(id):
article = f'<h2>{topic["title"]}</h2>{topic["body"]}'
return HttpResponse(HTMLTemplate(article, id))
@csrf_exempt
def create(request):
global nextId
print("request.method", request.method)
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":
print(request.POST['title'])
title = request.POST['title']
body = request.POST['body']
newTopic = {"id":nextId, "title":title, "body":body}
topics.append(newTopic)
url = '/read/'+str(nextId)
nextId+=1
return redirect(url)
@csrf_exempt
def delete(request):
global topics
if request.method == "POST":
id = request.POST['id']
newTopics = []
for topic in topics:
if topic['id'] != int(id):
newTopics.append(topic)
topics = newTopics
return redirect('/')
@csrf_exempt
def update(request, id):
global topics
if request.method == 'GET':
for topic in topics:
if topic['id'] == int(id):
selectedTopic = {
"title" : topic['title'],
"body" : topic['body'],
}
article =f'''
<form action="/update/{id}/" method="post">
<p><input type="text" name="title" value={selectedTopic["title"]}></p>
<p><textarea name="body">{selectedTopic["body"]}</textarea></p>
<p><input type="submit"></p>
</form>
'''
return HttpResponse(HTMLTemplate(article, id))
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
return redirect(f'/read/{id}')
생활코딩 강의