본문 바로가기
반응형

django7

Django 404에러 메시지 출력 from django.http import Http404from django.shortcuts import renderfrom .models import Question# ...def detail(request, question_id): try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") return render(request, 'polls/detail.html', {'question': question})이게 일반적인 404 에러 메시지 출력 코드그리고 아래가 숏컷 코드이다.from django.. 2025. 6. 10.
Django 이미지 1. App폴더 안에 templates 폴더 생성2. templates폴더 안에 App폴더와 같은 이름의 폴더 생성3. 생성한 templates/App폴더 안에 index.html 파일 생성touch 경로/index.html4. 보여질 화면을 만든다.index.html{% if latest_question_list %} {% for question in latest_question_list %} {{ question.question_text }} {% endfor %} {% else %} No polls are available.{% endif %}5. index클래스에 방금전에 index.html로 만든 화면을 불러오는 코드를 작성한다.polls/views.py.. 2025. 6. 10.
Django 어드민 1. 어드민 유저 만들기python manage.py createsuperuser 2. 서버를 실행시키 후, 주소 뒤에 /admin/을 추가하면 아래와 같은 어드민 화면이 보여진다.3. Question 오브젝트를 어드민 사이트에서 사용하려면 어드민 앱에게 Question 오브젝트가 어드민 인터페이스를 가지고 있다고 얘기해 줘야 합니다. polls/admin.pyfrom django.contrib import adminfrom .models import Questionadmin.site.register(Question) 4. 다시 어드민 주소로 들어가면 다음과 같이 POLLS 폴더가 보여진다. 2025. 6. 7.
Django 데이터베이스 * 장고는 sqlite 데이터베이스를 기본적으로 제공한다. 데이터베이스 테이블 설정python manage.py migrate 데이터베이스 필드&컬럼 설정polls/models.pyfrom django.db import modelsclass Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_l.. 2025. 6. 7.
반응형