[점프 투 장고] 02-6 에러가 납니다
점프투 장고에서 02-6 챕터 96페이지까지 진행했는데
Internal Server Error: /pybo/answer/create/1/
Traceback (most recent call last):
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.IntegrityError: NOT NULL constraint failed: pybo_answer.content
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\djangoworkspace\django1\pybo\views.py", line 28, in answer_create
question.answer_set.create(content=request.POST.get('content'), create_date=timezone.now())
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\fields\related_descriptors.py", line 677, in create
return super(RelatedManager, self.db_manager(db)).create(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 790, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 933, in _do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\sql\compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 79, in _execute
with self.db.wrap_database_errors:
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: NOT NULL constraint failed: pybo_answer.content
이라고 에러가 뜹니다. 어떻게 해야 할까요?
참고로 코드는
config/urls.py
from django.contrib import admin
from django.urls import path, include
from pybo import views
urlpatterns = [
path('admin/', admin.site.urls),
path('pybo/', include('pybo.urls')),
]
pybo/urls.py
from django.urls import path
from . import views
app_name = 'pybo'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('answer/create/<int:question_id>/', views.answer_create, name='answer_create'),
]
views.py
from django.shortcuts import render, get_object_or_404, redirect
from .models import Question
from django.utils import timezone
# Create your views here.
def index(request):
"""
pybo 목록 출력
"""
question_list = Question.objects.order_by('-create_date')
context = {'question_list': question_list}
return render(request, 'pybo/question_list.html', context)
def detail(request, question_id):
"""
pybo 내용 출력
"""
question = get_object_or_404(Question, pk=question_id)
context = {'question': question}
return render(request, 'pybo/question_detail.html', context)
def answer_create(request, question_id):
"""
pybo 답변 등록
"""
question = get_object_or_404(Question, pk=question_id)
question.answer_set.create(content=request.POST.get('content'), create_date=timezone.now())
return redirect('pybo:detail', question_id=question.id)
models.py
from django.db import models
class Question(models.Model):
subject = models.CharField(max_length=200)
content = models.TextField()
create_date = models.DateTimeField()
def __str__(self):
return self.subject
class Answer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
content = models.TextField()
create_date = models.DateTimeField()
admin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
search_fields = ['subject']
admin.site.register(Question, QuestionAdmin)
settings.py
(생략)
INSTALLED_APPS = [
'pybo.apps.PyboConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
(생략)
LANGUAGE_CODE = 'ko-kr'
TIME_ZONE = 'Asia/Seoul'
(생략)
apps.py
from django.apps import AppConfig
class PyboConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'pybo'
question_list.html
{% if question_list %}
<ul>
{% for question in question_list %}
<li><a href="{% url 'pybo:detail' question.id %}">{{ question.subject }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>질문이 없습니다.</p>
{% endif %}
question_detail.html
<h1>{{ question.subject }}</h1>
<div>
{{ question.content }}
</div>
<h5>{{ question.answer_set.count }}개의 답변이 있습니다.</h5>
<div>
<ul>
{% for answer in qustion.answer_set.all %}
<li>{{ answer.content }}</li>
{% endfor %}
</ul>
</div>
<form action="{% url 'pybo:answer_create' question.id %}" method="post">
{% csrf_token %}
<textarea name="contet" id="content" rows=15></textarea>
<input type="submit" value="답변 등록">
</form>
입니다.
seanleeee13 님 453
M 2023년 8월 4일 3:39 오후
댓글 1개 더 보기...
@박응용님 그러면 어디에 출력 코드를 넣으면 될까요?
-
seanleeee13님,
2023년 8월 6일 8:52 오후
추천
,
대댓글
@seanleeee13님 여러방법이 있겠지만 가장 쉽게는 view에서 print로 찍으면 콘솔에 출력됩니다.
-
박응용님,
2023년 8월 6일 8:54 오후
추천
,
대댓글
실행을 했는데 갑자기 에러가 나요
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 82, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 411, in execute
return Database.Cursor.execute(self, query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.DatabaseError: database disk image is malformed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check_migrations()
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 459, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
self.build_graph()
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
if self.has_table():
^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
tables = self.connection.introspection.table_names(cursor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
return get_names(cursor)
^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\introspection.py", line 74, in get_table_list
cursor.execute("""
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 79, in _execute
with self.db.wrap_database_errors:
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 82, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lecha\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 411, in execute
return Database.Cursor.execute(self, query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.DatabaseError: database disk image is malformed
-
seanleeee13님,
2023년 8월 6일 9:15 오후
추천
,
대댓글
+1
@seanleeee13님 데이터베이스 파일에 문제가 생긴것 같습니다. sqlite3 파일과 migrations 폴더를 삭제하시고 makemigrations 부터 다시 진행하셔야 할 것 같네요.
-
박응용님,
2023년 8월 6일 9:24 오후
추천
,
대댓글
@박응용님 test
-
daehoon.seong님,
2023년 8월 19일 12:00 오후
추천
,
대댓글