๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ4: ๐ต๐’ถ๐’ธ๐“€๐‘’๐“ƒ๐’น/๐“ก๐Ÿฆ๐Ÿข๐Ÿฃ:๐’Ÿ๐’ฟ๐’ถ๐“ƒ๐‘”๐‘œ

[Django] 5. ํ…œํ”Œ๋ฆฟ์˜ ๊ธฐ๋ณธ ์ดํ•ด

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)