Django

Django(hello>hello02)[3] for, if01, if02

FnMask 2020. 11. 21. 15:38

hello>hello>templates>for.html 을 만들어주세요

    {% for i in number %}
    <p>{{i}}</p>
    {% endfor %}

hello02>views.py

def forLoop(request):
    return render(request, 'for.html', {'number': range(1, 10)})
    #range(1, 10)을 템플릿 안에서 for i in range로 쓰면 안되는 이유:
    #django 템플릿은 화면에 뿌려주는 역할만 해서 쓸 수 없습니다.

hello02>urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.hello),
    path('var01', views.variable01),
    path('var02', views.variable02),
    path('for', views.forLoop),
]

hello02/for 경로로 들어가면 실행됩니다.

잘 됩니다!

-----------------------------------------------------------------------------------------------------------------------------------

 

hello>templates>if01.html을 만들어주세요

if01.html

{% if user.id %} <h1>Hello,{{user.name}}</h1> {% endif %}
#id가 있으면 user name을 출력해줄꺼에요

hello02>views.py

from django.shortcuts import render

# Create your views here.
def hello(request):
    return render(request, 'hello02.html', {'name': 'django template'})

def variable01(request):
    lst = ['python', 'django']
    return render(request, 'variable01.html', {'lst': lst})

def variable02(request):
    dct = {'class': 'qclass', 'name': '박보검'}
    return render(request, 'variable02.html', {'dct': dct})

def forLoop(request):
    return render(request, 'for.html', {'number': range(1, 10)})

def if01(request):
    return render(request, 'if01.html', {'user': {'id': 'qclass', 'name': 'teacher'}})

hello02/urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.hello),
    path('var01', views.variable01),
    path('var02', views.variable02),
    path('for', views.forLoop),
    path('if01', views.if01),	#if01로 요청했을 때 if01.html이 나옴
]

결과 화면

-----------------------------------------------------------------------------------------------------------------------------------

hello>templates>if02.html을 만들어주세요

hello>templates>if02.html

    {% if role == 'admin' %}
        <h1>AdminPage</h1>
        <a>user list</a>
        <a>user delete</a>
    {%elif role == 'manager' %}
        <h1>ManagerPage</h1>
        <a>user list</a>
    {% else %}
        <h1>UserPage</h1>
    {% endif %}

views.py

from django.shortcuts import render

# Create your views here.
def hello(request):
    return render(request, 'hello02.html', {'name': 'django template'})

def variable01(request):
    lst = ['python', 'django']
    return render(request, 'variable01.html', {'lst': lst})

def variable02(request):
    dct = {'class': 'qclass', 'name': '박보검'}
    return render(request, 'variable02.html', {'dct': dct})

def forLoop(request):
    return render(request, 'for.html', {'number': range(1, 10)})

def if01(request):
    return render(request, 'if01.html', {'user': {'id': 'qclass', 'name': 'teacher'}})

def if02(request):
    return render(request, 'if02.html', {'role': 'manager'})

urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.hello),
    path('var01', views.variable01),
    path('var02', views.variable02),
    path('for', views.forLoop),
    path('if01', views.if01),
    path('if02', views.if02),
]

결과화면

출력되는지 확인해주세요

'Django' 카테고리의 다른 글

Django(hello>hello02)[4] href  (0) 2020.11.21
Django(hello>hello02)[2]  (0) 2020.11.21
Django(hello>hello02)[1]  (0) 2020.11.21
Django(hello/hello01)  (0) 2020.11.21
Django (hello/hello)  (0) 2020.11.21