from django.templates import loader
def index(request):
templates = loader.get_template('index.html')
context = {}
return HttpResponse(template.render(context, request))
#settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first',
]
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>Title</title>
</head>
<body>
<p>Hello World!</p>
<p>{{ current_date}}</p>
<p>{{ current_date|date:"Y๋
m์ d์ผ H์ i๋ถ s์ด"}}</p>
</body>
</html>
def index(request):
template = loader.get_template('index.html')
now = datetime.now()
context = {
'current_date' : now
}
return HttpResponse(template.render(context, request))
<a href="{% url 'select' %}">์์ํ๊ธฐ!</a>
from django.shortcuts import render
--> shortcut ์ฝ๋ฉ
def index(request):
now = datetime.now()
context = {
'current_date' : now
}
return render(request, 'index.html', context)
def select(request):
context = {}
return render(request, 'select.html', context)
def result(request):
context = {}
return render(request, 'select.html', context)
#select.html
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>Title</title>
</head>
<body>
<p>
<a href="{% url 'result' %}"><button>๊ฒฐ๊ณผ๋ณด๊ธฐ</button></a>
</p>
</body>
</html>
views - select
def select(request):
context = {'number' : 4}
return render(request, 'select.html', context)
#select.html
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>Title</title>
</head>
<body>
<p>
{% if number|divisibleby:2 %}
์ง์์
๋๋ค.
{% else %}
ํ์์
๋๋ค.
{% endif %}
</p>
<p>
<a href="{% url 'result' %}"><button>๊ฒฐ๊ณผ๋ณด๊ธฐ</button></a>
</p>
</body>
</html>
views.py
def result(request):
context = {'numbers' : [1,2,3,4,5,6]}
return render(request, 'result.html', context)
views.html
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>Title</title>
</head>
<body>
<ul>
{% for num in numbers %}
<li>{{ num }}</li>
{% endfor %}
</ul>
</body>
</html>
static ํ์ผ ๋์ฐ๊ธฐ
#settings
import os
STATIC_URL = '/static/'
STATICFILES_DIRS = {
os.path.join(BASE_DIR, "static")
}
index.html
{% load static %}
<!DOCTYPE html>
<html lang = "ko">
<head>
<meta charset = "UTF-8">
<title>Title</title>
</head>
<body>
<p>Hello World!</p>
<img style="width:100px;" src="{% static 'image.jpg' %}" alt="๋ค๋์ฅ"/>
<p>{{ current_date}}</p>
<p>{{ current_date|date:"Y๋
m์ d์ผ H์ i๋ถ s์ด"}}</p>
<a href="{% url 'select' %}">์์ํ๊ธฐ!</a>
</body>
</html>
---
<img style="width:100px;" src="{% static 'first/image.jpg' %}" alt="๋ค๋์ฅ"/>
def index(request):
now = datetime.now()
context = {
'current_date' : now
}
return render(request, 'first/index.html', context)
def select(request):
context = {'number' : 4}
return render(request, 'first/select.html', context)
def result(request):
context = {'numbers' : [1,2,3,4,5,6]}
return render(request, 'first/result.html', context)
block ๊ฐ๋
#base.html
{% load static %}
<!DOCTYPE html>
<html lang = "ko">
<head>
<meta charset = "UTF-8">
<title>๋ก๋ ๋ฒํธ</title>
<link rel="stylesheet" type = "text/css" href = "{% static 'first/styles.css' %}">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
index.html
{% extends 'first/base.html' %}
{% load static %}
{% block content %}
{% extends 'first/base.html' %}
{% load static %}
{% block content %}
<p>Hello World!</p>
<img style = "width : 100px;" src="{% static 'first/image.jpg' %}" alt="๋ค๋์ฅ"/>
<p>{{ current_date}}</p>
<p>{{ current_date|date:"Y๋
m์ d์ผ H์ i๋ถ s์ด"}}</p>
<a href="{% url 'select' %}">์์ํ๊ธฐ!</a>
{% endblock %}
#select.html
{% extends 'first/base.html' %}
{% block content %}
<p> ์๋ฅผ ํ๋ ์
๋ ฅํด์ฃผ์ธ์.</p>
<form action="{% url 'result' %}" method = "get">
<input type = "number" name="number"/>
<button type = "submit">๊ฒฐ๊ณผ๋ณด๊ธฐ</button>
</form>
{% endblock %}
#views.py - result
def result(request):
chosen = request.GET['number']
context = {
'numbers' : [chosen, 2, 3, 4, 5, 6]
}
return render(request, 'first/result.html', context)
#result.html
{% extends 'first/base.html' %}
{% block content %}
<h3> ์ถ์ฒ ๋ฒํธ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.</h3>
<div style="text-align:left;">
<ul>
{% for num in numbers %}
<li>{{ num }}</li>
{% endfor %}
</ul>
</div>
{% endblock %}
=== ์ ๋๋ก ๋ result ๊ตฌํ
def result(request):
chosen = int(request.GET['number'])
results = []
if chosen >= 1 and chosen <=45:
results.append(chosen)
box = []
for i in range(0,45):
if chosen != i+1:
box.append(i+1)
random.shuffle(box)
while len(results)<6:
results.append(box.pop())
context = {
'numbers' : results
}
return render(request, 'first/result.html', context)
'๐ก๐ธ๐ธ๐ถ4: ๐ต๐ถ๐ธ๐๐๐๐น > ๐ก๐ฆ๐ข๐ฃ:๐๐ฟ๐ถ๐๐๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Django] 6. model์ ์ดํด (0) | 2020.12.22 |
---|---|
[Django] 4. ํ์ด์ง ๋์ฐ๊ธฐ(views์ urls์ ๊ฐ๋จํ ์ดํด) (0) | 2020.12.05 |
[Django] 3. Django ํ๋ก์ ํธ ์์ฑ (0) | 2020.12.05 |
[Django] 2. vscode Django ์ธํ (0) | 2020.12.05 |
[Django] 1. Web์ ๊ธฐ๋ณธ ์ดํด (0) | 2020.12.05 |