티스토리 뷰

파이썬 에러 트러블슈팅 - UnicodeDecodeError: 'cp949' codec can't decode byte * in position *: illegal multibyte sequence

 

원인 : 인코딩과 디코딩 충돌이나 혹은 파일이 0kb 일 수도 있으니 기타 IDE 로 열어보기

 

아래와 같이 실행할 경우에 

import json

with open('genre.json', 'r') as f:
    genre_list = json.load(f)
print(genre_list)

 

genre.json

["sf", "가족", "공표", "드라마", "로맨스", "먼치킨", "모험", "미스테리", "범죄", "서스펜스", "스포츠", "시대", "옴니버스", "요괴", "이세계", "일상", "전투", "코미디", "판타지", "학원"]

 

UnicodeDecodeError: 'cp949' codec can't decode byte 0x80 in position 10: illegal multibyte sequence

 

에러가 발생했다. 

 

아래와 같이 실행하면 출력이 잘 된다.

import json

with open('genre.json', 'rt', encoding='UTF-8') as f:
    genre_list = json.load(f)
print(genre_list)

 

 

 

참조 : 

py - [파이썬 에러 해결] UnicodeDecodeError: 'cp949' codec can't decode byte * in position *: illegal multibyte sequence

 

 


Remote branch 이름 변경하기

 

- Remote branch 의 이름은 직접적으로 변경할수 없으니 아래와 같은 방법을 사용한다.

 

1. Local 에 있는 branch 이름 변경하기

git branch -m oldbranch newbranch

 

GitHub나 원격 저장소의 변경은 git push를 활용합니다.

$ git push origin newbranch
$ git push origin --delete oldbranch

두개를 한번에

$ git push origin :old_branch new_branch

 

참조 :

LainyZine : 프로그래머 가이드 - GitHub 저장소의 브랜치 이름을 변경하는 방법

 


 

Url - 장고 탬플릿 태그 Advanced Usage

 

앱의 뷰 클라이언트를 가정하고 이 클라이언트가 URLConf 가 클라이언트 아이디를 취했다. 

URLconf 는 아래처럼 보일 것이다. 

더보기

suppose you have a view, app_views.client, whose URLconf takes a client ID (here, client() is a method inside the views file app_views.py). The URLconf line might look like this:

path('client/<int:id>/', app_views.client, name='app-views-client')

만약 앱의 URLconf 가 아래와 같이 프로젝트의 URLconf를 포함해서 패스를 아래처럼 보여준다고 가정하면

path('clients/', include('project_name.app_name.urls'))

템플릿에서 아래와 같이 보이는 뷰로 향하는 링크를 만들것이다.

{% url 'app-views-client' client.id %}

탬플릿 태그는 /clients/client/123/ 로 출력될 것이다.

The template tag will output the string /clients/client/123/.

 

참조 :

url – Django Template Tag


TIL_20221006

 

url - 장고 탬플릿 태그

 

url 태그는 절대 경로를 반환한다. 

 

예시)

탬플릿을 통해 접근할 수 있는 2개의 뷰를 만든다.

# import Http Response from django
from django.shortcuts import render

# create a function
def a_test(request):
	# return response
    return render(request, "a.html")

def b_test(request):
	# return reponse
    return render(request, "b.html")

이 뷰로 향하는 url path 를 만든다. URLs 는 url tag가 함께 있는 템플릿 안에서 사용되는 이름이 있어야 한다.

from django.urls import path

# importing views from views.py

urlpatterns = [
	path('1/', a_test, name="template1"),
	path('2/', b_test, name="template2"),
]

이제 tag를 증명하는 2개의 템플릿을 만들어 보자

아래는 a.html 이다.

<html>
<h1> a_test view </h1>
<h1> Tmeplate 1</h1>
<a href = "{% url 'template2 %}">Go to template 2</a>
</html>

여기에서 템플릿2로 연결하는 b.html 을 만들어보자

<html>
<h1> b_test view </h1>
<h2> Templatea 2 </h2>
<a href = "{% url 'template1' %}">Go to template 1</a>
</html>

이제 로컬 서버를 구동해서 http://127.0.0.1:8000/1 로 접속을 해보자.

태그로 실행된 a.html

위의 링크를 통해 템플릿 2로 이동

 

태그로 실행된 b.html

 

참조 : 

url – Django Template Tag


 

오늘 한일

- 머신러닝 강의 수강

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함