Python
-
[Python] Django urls.py, views.py 초기 세팅Python/Django 2021. 11. 30. 17:21
# urls.py of project foler from django.contrib import admin from django.urls import path, include from first_app import views urlpatterns = [ path('admin/', admin.site.urls), path('', include('first_app.urls')), ] # urls.py of app folder from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
-
[Python] TypeError: __init__() missing 1 required positional argument: 'on_delete'Python/Django 2021. 11. 30. 16:19
class Webpage(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE,) name = models.CharField(max_length=264, unique=True) url = models.URLField(unique=True) Django 2.0 이후 부터는 ForeignKey를 사용할 경우, 2개의 파라미터를 받게 되어있다. on_delete를 사용할 경우에 대한 설정
-
[Python] text를 index해보자 module : rePython 2021. 11. 24. 22:00
import re text = "Let me grab a coffee real quick" match = re.search("grab", text) print(match) => print(match.span()) => (7, 11) # 위치를 반환 Match = re.search("Hello World", text) print(Match) => None import re split_term = "@" email = "user@gmail.com" print(re.split(split_term, email)) => ['user', 'gmail.com'] https://docs.python.org/ko/3/library/re.html re — 정규식 연산 — Python 3.10.0 문서 scanf() 시뮬레..
-
[Python] with문에 대해 알아보자 (실행과 종료)Python 2021. 11. 24. 21:51
with statement: __enter__ 와 __exit__ 특수메소드를 가진 객체에 with 문을 쓸 수 있다. 자동으로 파일에 접근하고 수행하고 닫아준다. example: with {expression} as {variable}: 실행 with open('textfile.txt', 'r') as file: contents = file.read() # 위 구문과 동일한 내용 file = open('textfile.txt', 'r') contents = file.read() file.close()
-
[Python] 예외 처리를 해보자 try, except, else, finallyPython 2021. 11. 24. 21:39
from os import error try: f = open("text.txt", "w") f.write("Test write to a text") => 일단 한번 실행해봄 except error as err: print(f"ERROR: {err}") => 에러가 발생 했다면 실행 else: print("I work when error has not been raised") => 에러가 발생 하지 않았다면 실행 finally: print("I always work no matter what") => 이러든 저런든 어쨋든 실행