목록Web /Django (22)
알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)
Apache2에서 Django 배포 재작성의 이유 최근에 python 버전의 다양화에 따른 mod_wsgi 의 실행 환경의 변화 Django의 많은 버전 업그레이드 DjangoRestFrameWork 인증 이슈(JWT 포함) ubuntu 에서 설정 디테일 하게 하기 위해. 위와 같은 이유로 배포 절차를 다시 작성 하게 되었습니다. Django Project 기본 정보 프로젝트 이름 : bwaferMap static 파일 경로: static collectstatic 경로: staticfiles settings.py STATIC_URL = '/bWaferMap/static/' STATICFILES_DIRS = [ BASE_DIR / 'static' ] STATIC_ROOT = os.path.join(BASE..
bootstrap 테마 적용 저게 예제로 사용할 탬플릿은 https://startbootstrap.com/template-overviews/shop-homepage/ 에 있는 테마 입니다. 다운로드 받으면 위와 같은 내용 들이 있습니다. 이를 Django에 적용 하려고 합니다. 먼저 저의 Django 환경은 project 이름은 Vcsite app이름은 mainapp 으로 만들었습니다. 먼저 Vcsite/mainapp/ 에 static 폴더를 만들고 Vcsite/mainapp/static 에 mainapp 이라고 폴더를 만들어 줍니다. Vcsite/mainapp/static/mainapp 에 자신이 받은 bootstrap 파일중 index.html을 제외하고 모두 복사 붙혀 넣기 합니다. 저의 경우에 a..
css 적용 Django 에서는 css 파일을 static 요소 라고 합니다. Django 에서는 setting.py -> STATICFILES_FINDERS 에 따라 static 파일을 만드는 경로에 규칙이 존재합니다. 지금까지 만들어 왔던 polls/ 에 static 폴더를 만들어 줍니다. 그밑에 다시 polls 폴더를 만들어 주고 style.css 라고 파일을 만들어 줍니다. polls/static/polls/style.cssli a { color: green;} 이후에 $ python manage.py collectstatic 실행 시켜 줍니다. 이렇게 되면 mysite/setting.py STATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR, ..
TEST 하기 여기서 테스트 할것은 polls/model.py 의 Question 의 함수에서 was_published_recently()가 Question.pub_date 가 오늘 날짜인지 구분하는 함수를 만들것 입니다. 테스트를 위해 일부러 잘못된 코드를 작성 하도록 할것 입니다. polls/models.py from django.db import models import datetime from django.utils import timezone # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date..
제너릭 뷰 URL에서 전달 된 매개 변수에 따라 데이터베이스에서 데이터를 가져 오고 이것을 view에서 rander 함수 등을 통해 템플릿에 리턴하는 기본 웹 개발의 일반적인 경우를 자주 사용하게 되면서 django 에서는 더욱더 적은 코드로 사용할수 있도록 제너릭 뷰라는 것을 제공합니다. 대표적인 제너릭 뷰에서 generic.ListView 와 generic.DetailView 가 있습니다. 먼저 views.py를 먼저 보겠습니다. polls/views.py from django.shortcuts import get_object_or_404, render # Create your views here. from django.http import HttpResponseRedirect,HttpResponse..
HttpResponseRedirect 현재 App에서 다른 App 으로 이동을 해주는 url Redirect 을 해준다. 이전 강의에서 이어 가도록 하겠습니다. polls/views.py from django.shortcuts import get_object_or_404, render # Create your views here. from django.http import HttpResponseRedirect,HttpResponse from .models import Question,Choice from django.urls import reverse def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] ..
django 간단한 폼 만들기 일단 예제 코드를 본다음 1개씩 분석을 해보도록 한다. 예제에 앞서 polls/views.py 을 보겠다 polls/views.py from django.shortcuts import get_object_or_404, render # Create your views here. from django.http import HttpResponse from .models import Question def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(req..
Template 에서 하드코딩된 URL 을 제거 코딩을 하다보면 템플릿에 아래와 같은 코드를 사용했을때에 {{ question.question_text }} /polls/ 의 url 경로가 바뀌면 /polls/ 의 경로가 들어간 템플릿을 모두 찾아 url 변경을 해줘야 합니다. 적을때는 문제가 안되지만 프로젝트 규모가 커지고 소스 량이 많아질때는 대단히 공수가 많이 들어가는 작업 입니다. 그래서 {{ question.question_text }} 이런식으로 바꿔 쓸수가 있습니다. url 'detail' 은 polls/urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name=..
Template 시스템 사용하기 poll 어플리케이션의 detail() view 로 되돌아 가봅시다. context 변수 question 이 주어졌을때, polls/detail.html 라는 template 을 만들고 어떻게 보이는지 테스트 해보도록 하겠습니다. polls/templates/polls/detail.html{{ question.question_text }} {% for choice in question.choice_set.all %} {{ choice.choice_text }} {% endfor %} 일단 이렇게 적어 줍니다. 데이터를 따라 가보겠습니다. question.question_text 은 polls/views.py from django.shortcuts import get_obj..
django 404 에러 일으키기 polls/views.py from django.shortcuts import render # Create your views here. from django.http import HttpResponse from .models import Question from django.template import loader from django.http import Http404 def detail(request,question_id): try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") return rende..