본문 바로가기

반응형

Django

(37)
Django TDD beautifulsoup4 사용하기 Django에는 화면 Test를 자동화 해주는 도구가 있다. pip install beautifulsoup4 이를 이용하면 화면을 브라우저로 실행해 보지 않아도 테스트가 가능하다.
Pillow Python에서 ImageField를 사용하기 Pillow는 Python에서 Image 처리를 하기 위해 사용되는 Library 입니다. 설치 방법 pip install Pillow
django TDD Test 실행 방법 python manage.py test django의 각 app에는 tests.py가 있고 거기에 Test Source를 작성하면 됩니다. class TestView(TestCase): def test_post_list(self): self.assertEqual(2, 3)​ 결과 2는 3이 아니므로 Test는 Fail이 됩니다. $ python manage.py test Found 1 test(s). Creating test database for alias 'default'... System check identified no issues (0 silenced). F =================================================================..
Django shell을 이용하여 source 실행 시키기 예제 1 $ python manage.py shell Python 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from blog.models import Post >>> p = Post.objects.last() >>> p.title '시간 자동 저장' >>> p.create_at datetime.datetime(2022, 2, 17, 12, 48, 24) >>> p.update_at datetime.datetime(2022..
Django view에서 넘기지 않은 key 값을 구분하기 if문과 None 키워드를 사용하여 서버에서 넘어오지 않은 값을 구분할 수 있다. {% if js_url != None %} {% endif %}
Django 주석 달기 한줄 주석 단순히 앞에 # 만 붙이면 됩니다. #return HttpResponse("Hello World!") 여러줄 주석 '''를 사용합니다. ''' template = loader.get_template('HelloWorld/index.html') context = {} return HttpResponse(template.render(context, request)) '''
Django 모델이 생성된 후에 default date 컬럼 추가하기 모델에 DateTimeField 추가 create_at = models.DateTimeField(default=timezone.now()) makemigrations를 이용하여 DB Schema 생성 준비 이 때 기존에 row가 있기 때문에 이를 어떤 값으로 채울지 결정하라고 한다. timezone.now를 입력하여 현재 시간으로 기존 로우에 대한 값을 설정한다. $ python manage.py makemigrations System check identified some issues: WARNINGS: HelloWorld.Post.create_at: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / tim..
Queryset 역순으로 정렬하기 (How to reverse the order of query result) order_by 함수를 이용할 때 Column ID 앞에 - 를 붙이면 역순으로 정렬 된다. def post_detail(request, post_id): post = Post.objects.get(pk=post_id) comment = Comment.objects.filter(post__id=post_id).order_by('-id') return render(request, "HelloWorld/post_detail.html", {'post': post, 'comments':comment})

반응형