일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 백준
- java #자바 #동빈나
- 투포인터
- react #리액트 #동빈나
- 다이나믹프로그래밍
- 코딩테스트
- dp
- Dijkstra
- css #생활코딩 #웹
- 알고리즘
- BFS
- 파이썬 #백준 #알고리즘 #코딩테스트
- 자바 #java
- 파이썬
- 프로그래머스 #파이썬 #코딩테스트 #알고리즘
- 파이썬 #알고리즘 #코딩테스트 #프로그래머스
- 백트랙킹
- css #웹 #생활코딩
- 백준 #알고리즘 #파이썬 #코딩테스트
- 프로그래머스 #파이썬 #알고리즘 #코딩테스트
- 재귀
- java #자바 #생활코딩
- PYTHON
- 프로그래머스
- java #자바
- 다익스트라
- DFS
- react #리액트 #동빈나 #나동빈 #유튜브강의
- 백준 #파이썬 #알고리즘 #코딩테스트
- java #자바 #나동빈
- Today
- Total
목록
728x90
django
728x90
(25)
커리까지
[test request 만들기] 장고에 존재하는 RequestFactory class를 확장해서 사용한 APIRequestFactory 클래스를 사용하자. 기본적인 get(), .post(), .put(), .patch(), .delete(), .head() and options()을 사용할 수 있다. [기본적인 test 코드] 아래와 같이 기본적인 구조를 따르면서 작성하자. from django.contrib.auth.models import AnonymousUser, User from django.test import RequestFactory, TestCase from .views import MyView, my_view class SimpleTest(TestCase): def setUp(self..
ViewSets 사용하기 restapi > appapi > views.py List, Detail을 나누었던 것을 하나로 합쳐서 사용할 수 있다. class UserViewSet(viewsets.ReadOnlyModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer ReadOnlyModelViewSet 는 list, retrieve 를 함께 사용할 수 있다. class SnippetViewSet(viewsets.ModelViewSet): queryset = Snippet.objects.all() serializer_class = SnippetSerializer permission_classes = [permissions...
endpoint for the root of our API 만들기 이전에 만들었던 api는 endpoint가 snippet, user 각각 2개였다. 이걸 하나의 endpoint로 만들자. restapi > appapi > views.py from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework.reverse import reverse @api_view(['GET']) def api_root(request, format=None): return Response({'users': reverse('user-list', req..
1 ~3장까지의 rest api는 누구나 접근해서 조회하거나 생성, 수정, 삭제할 수 있었다. 이러한 자유성에 제한을 줘보자. 모델에 필드 추가하기 restapi > appapi > models.py from pygments.lexers import get_lexer_by_name from pygments.formatters.html import HtmlFormatter from pygments import highlight class Snippet(BaseModel): title = models.CharField(max_length=100, blank=True, default='') code = models.TextField() linenos = models.BooleanField(def..
view 세팅 restapi > appapi > views.py class SnippetList(APIView): def get(self, request, format=None): snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) return Response(serializer.data) def post(self, request, format=None): serializer = SnippetSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status...
api_view, status restapi > appapi > views.py 데코레이터로 응답 형태를 지정할 수 있다. 리턴되는 상탯값을 status모듈에서 사용할 수 있다. from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response from .models import Snippet from .serializers import SnippetSerializer @api_view(['GET', 'POST']) def snippet_list(request): if request.method == 'GET&#..
Veiw 설정 함수 기반으로 작성 이번에는 REST framework 대신 일반적인 django view를 사용해서 구현 restapi > appapi > views.py 먼저 필요한 패키지를 선언한다. from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from rest_framework.parsers import JSONParser from .models import Snippet from .serializers import SnippetSerializer snippet를 조회하는 def를 생성한다. @csrf_exempt def snippet_list(request)..
프로젝트 구조 프로젝트 이름 app 이름 restapi appapi 설치하기 공식 사이트에 나와있는 설명서를 참고하여 설치 pip install django pip install djangorestframework pip install pygments # We'll be using this for the code highlighting pygments는 코드 강조기능 App 추가하기 restapi > restapi > settings.py INSTALLED_APPS에 rest_framework, appapi 를 추가한다. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'djang..
장고에서 form으로 체크박스를 받아오면 해당 체크박스 마지막만 받아와졌다. 체크된 값 모두를 받으려면 다음과 같이 views.py를 설정해준다. {% csrf_token %} 제출 우선 name은 기존처럼 model에서 설정한 이름대로 설정해도 무방하다. def input_test(request): if request.POST: list_item = request.POST.getlist('test') print(list_item) 여기서 checkbox의 체크된 값을 모두 가져오려면 getlist로 값을 받아오면 된다. 위 사진처럼 1,2,3의 값을 체크하고 제출을 클릭하면 리스트로 체크된 value값이 들어온다. cbv로 바꿔서 db에 저장하기 리스트로 들어오는 것을 확인했으면 db에 저장할 때는 어..