본문 바로가기

반응형

전체 글

(202)
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..
Github에 잘 못 올라간 파일 삭제하기 --cached를 이용해서 github server의 파일 만 삭제할 수 있다. $ git rm --cached db.sqlite3 rm 'db.sqlite3' $ git rm --cached SunnyBlog/settings.py rm 'SunnyBlog/settings.py' 이후에 git status 확인하고 Commit 하고 push 하면 로컬에는 남아 있고 github에서는 지워진다. $ git status On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git restore --staged ..." to unstage) deleted: SunnyBlog/settings.py de..
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})
Django ForeignKey 데이터 가져오기 https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_one/ Many-to-one relationships | Django documentation | Django Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate docs.djangoproject.com Comment와 Post는 Many to One 관계이다. 이런 경우 특정 Post에 대한 Comment 전체를 가져오려면 아래와 같이 코딩한다. comment = Comment.objects.filte..
Django Models, Create Table, ForeignKey https://docs.djangoproject.com/en/4.0/topics/db/models/ Models | Django documentation | Django Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate docs.djangoproject.com Django는 Models에서 Data field를 정의하게 된다. Model을 정의하면 자동으로 Django가 DB Table을 생성 및 관리 한다. class Post(models.Model): post_title = models.CharField(max_l..
Django csft token with Ajax https://docs.djangoproject.com/en/4.0/ref/csrf/ Cross Site Request Forgery protection | Django documentation | Django Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate docs.djangoproject.com 폼을 이용하는 경우 폼 tag 사이에 {% csft_token %} 을 넣어주면 된다. {% csrf_token %} Ajax를 이용하는 경우 javascript로 csrftoken 값을 가져와서 X-CSRFToken 값..
git 초기 설정 1. git repository를 생성하고자 하는 디렉토리에서 git init 명령이 입력 $ git init hint: Using 'master' as the name for the initial branch. This default branch name hint: is subject to change. To configure the initial branch name to use in all hint: of your new repositories, which will suppress this warning, call: hint: hint: git config --global init.defaultBranch hint: hint: Names commonly chosen instead of 'master..

반응형