# 장고 Templates

장고 장고 아직은 장고

저번에 했던거에서 다시 시작할려했는데...?

**"message": "원본에서 가져오기 \\"django.contrib\\"을(를) 확인할 수 없습니다.",**

아니 코드에서 장고를 찾을수 없다고 하는데..? 난 분명히 장고를 다운 받았는데

혹시나 해서 장고를 다운다시 다운 받았지만.. 그래도 해결이 X

그러면 다음에 찾은 해결 방법이! 처음부터 삭제하고 다시 설치하기...  
하지만 이것도 해결이 X

설치하다가 에러가 나서 확인해봤더니  
**WARNING: You are using pip version 20.2.1; however, version 24.0 is available. You should consider upgrading via the 'c:\\users\\uyeon\\desktop\\　　　\\code\\git\\django\\venv\\scripts\\python.exe -m pip install --upgrade pip' command.**

pip 버전이 많이 바겼네?

**python -m pip install --upgrade pip**

그래서 pip을 업그레이드하고

다시 해보니까 해결이 되었다!

### 이제 Templates 해봅시다

1. 요청(Request)이 들어오면
    
2. URL([urls.py](http://urls.py)) 처리
    
3. View([views.py](http://views.py)) 처리
    
4. Template(html) 처리
    
5. 응답(Response) 전달
    

서버에 보여주는 경로! 코드 만들때 중요하다

1. 저번에 만들었던 파일 my\_first\_pjt에서 urls.py를 찾는다  
    urls.py에 url을 만든다
    
    ```python
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('index/', views.index), - 만든거
        path('users/', views.users), - 만든거
    ] 장고에서는 index <- 이거보단 index/를 권장한다
    ```
    
2. 이제 보여줘야하니까 내가만든 앱에 VIews.py를 찾는다  
    이제 내가만들었던 views를 보여줘야한다
    

```python
from django.shortcuts import render

def index(request):
    return render(request, "index.html") - return 한다 index.html 에있는걸
                                           하지만 파일이 없으니 파일을 만든다

def users(request):
    return render(request, "users.html")
```

3. index.html을 만들러가자  
    **my\_first\_pjt/앱이름/templates 여기에 index.html 파일을 만들고 여기 안에서  
    html을 꾸민다**
    

그리고 화면이 404오류가 나면 주소 뒤에 / 을 붙히고 index(이름)를 넣는다

### Django Templates System

HTML

```xml
 <h1>Hello, {{ name }} - 변수 !</h1>
    <P> 모든 태그 : {{ tags|join:"," }}</P> - 태그 --- ㅣ<-필터 대문자 및 글자체 바꾸기
    <p> 대표 태그 : {{ tags.0 }} (글자수 : {{ tags|length}})</p> - 태그
    <p> 최근 게임:{{ game.today}} - 변수 </p>
    <p> 플레이한 게임</p> - 변수
    <ul>
        {% for game in game.items %} -반복문 시작
            <li>{{ game.0 }} - {{ game.1 }}</li>
        {% endfor %} -반복문 끝
    </ul>
```

1. 변수로 넣기! {{ 변수 이름 }}
    
2. 그전에 Views.py 에서 바꿔줘야함!
    

```python
def hello(request):
    name = "우영" - 변수
    tags = ["#활발한", "#많이힘듬"] -태그
    game = {
        "today": "구스구스덕",
        "yesterday": "롤",
        "tomorrow": "GTA5",
    } - 변수

    context ={
        "name": name,
        "tags": tags,
        "game": game,

    }
    return render(request, "hello.html", context)
```

```html
{% extends 'base.html' %}    extends 태그를 이용해서 상속할 상위 템플릿 지정
{% block content%} block 태그를 이용해서 달라진 부분만 명시
{% endblock content %} endblock 로 끝내기
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712653179977/4b155d4b-718f-4965-a0c4-69137f4b99ed.png align="center")

오 이제 잘되는구만

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712653237764/bcfeb739-e6ba-4d39-bdda-fb4f4f5d87db.png align="center")

아 그냥 읽으면 base.html을 못찾는다고 하네..ㅏㅏㅏㅏㅏㅏㅏ  
여기서 해결 방법!  
**Django가 템플릿을 참조하는 위치는**[**settings.py**](http://settings.py)**에서 결정됨  
DIRS에 Django가 추가적으로 템플릿을 참조할 위치를 추가할 수 있다고함**

그럼 옮긴 곳을 추가를 해야지

1. my\_first\_pjt 최상단 경로에 templates 디렉토리 생성
    
2. base.html을 만든 templates에 넣는다
    
3. 이제 Django가 우리가 새로 생성한 templates 에서도 템플릿을 참조할 수 있게 적어주면 된다
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712653851070/2aa359e1-f894-49c3-9924-b2593c57ca78.png align="center")

DIRS 저기에는 \[\] 아무것도 안적혀져 있지만 \[BASE\_DIR / 설치한 경로\]를 써주면  
해결이 된당

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712654281576/7749a9c0-aeae-4f0b-a08d-ffdb411f87c0.png align="center")

바바는 바바야 바바는 바바야 바바는 바바야 바바는 바바야 바바는 바바야 바바는 바바야
