답변을 달면 IntegrityError가 나타납니다. 저번에도 발생해서 무시하고 지나갔는데...
다음에 진행하면서 수정할려고 했는데 제대로 안되서 인지 답변만 달면 다음과 같은 에러가 나타납니다.
저와 비슷한 에러가 난 질문자가 있어 그 부분을 봤는데 저는 오타가 아니었습니다. 코드가 길어지니 찾기가
난망합니다.
IntegrityError at /pybo/answer/create/187/
NOT NULL constraint failed: pybo_answer.question_id
Request Method: POST
Request URL: http://127.0.0.1:8000/pybo/answer/create/187/
Django Version: 3.1.3
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: pybo_answer.question_id
Exception Location: F:\big7\django\jumptodjango\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable: F:\big7\django\jumptodjango\Scripts\python.exe
Python Version: 3.8.5
Python Path:
['F:\\big7\\django',
'C:\\Users\\wonnh\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
'C:\\Users\\wonnh\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
'C:\\Users\\wonnh\\AppData\\Local\\Programs\\Python\\Python38\\lib',
'C:\\Users\\wonnh\\AppData\\Local\\Programs\\Python\\Python38',
'F:\\big7\\django\\jumptodjango',
'F:\\big7\\django\\jumptodjango\\lib\\site-packages']
Server time: Thu, 11 Mar 2021 06:57:20 +0000
pybo/views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import Question
from django.utils import timezone
from .forms import QuestionForm,AnswerForm
from django.core.paginator import Paginator
def index(request):
"""
pybo list
question_list=Question.objects.order_by('-create_date')
context={'question_list':question_list}
"""
page=request.GET.get('page','1')
question_list=Question.objects.order_by('-create_date')
paginator=Paginator(question_list,10)
page_obj=paginator.get_page(page)
#context={'question_list':question_list}
context={'question_list': page_obj}
return render(request,'pybo/question_list.html',context)
def detail(request,question_id) :
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 answer list
"""
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)
#return redirect('pybo:detail',question_id=question.id)
def question_create(request):
if request.method =='POST':
form=QuestionForm(request.POST)
if form.is_valid():
question=form.save(commit=False)
question.author=request.user
question.create_date=timezone.now()
question.save()
return redirect('pybo:index')
else:
form=QuestionForm()
context={'form':form}
return render(request,'pybo/question_form.html', context)
def answer_create(request,question_id):
question=get_object_or_404(Question, pk=question_id)
if request.method =='POST':
form=AnswerForm(request.POST)
if form.is_valid():
answer=form.save(commit=False)
answer.create_date=timezone.now()
answer.save()
answer.author=request.user
return redirect('pybo:detail',question_id=question.id)
else:
form=AnswerForm()
context={'form':form,'question':question}
return render(request,'pybo/question_detail.html', context)
pybo/urls.py
from django.urls import path
from . import views
app_name='pybo' #namespace
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'),
path('question/create/', views.question_create, name='question_create'),
]
#pybo/models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Question(models.Model):
auther=models.ForeignKey(User,on_delete=models.CASCADE)
subject=models.CharField(max_length=200)
content=models.TextField()
create_date=models.DateTimeField()
class Answer(models.Model):
auther = models.ForeignKey(User, on_delete=models.CASCADE)
question=models.ForeignKey(Question, on_delete=models.CASCADE)
content=models.TextField()
create_date=models.DateTimeField()
question_detail.html
{% extends 'base.html' %}
{% block content %}
<div class="container my-3">
<h2 class="border-bottom py-2">{{ question.subject }}</h2>
<div class="card my-3">
<div class="card-body">
<div class="card-text" style="white-space: pre-line;">{{ question.content }}</div>
<div class="d-flex justify-content-end">
<div class="badge badge-light p-2">
{{ question.create_date }}
</div>
</div>
</div>
</div>
<h5 class="border-bottom my-3 py-2">{{question.answer_set.count}}개의 답변이 있습니다.</h5>
{% for answer in question.answer_set.all %}
<div class="card my-3">
<div class="card-body">
<div class="card-text" style="white-space: pre-line;">{{ answer.content }}</div>
<div class="d-flex justify-content-end">
<div class="badge badge-light p-2">
{{ answer.create_date }}
</div>
</div>
</div>
</div>
{% endfor %}
<form action="{% url 'pybo:answer_create' question.id %}" method="post" class="my-3">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger" role="alert">
{% for field in form %}
{% if field.errors %}
<strong>{{ field.label }}</strong>
{{ field.errors }}
{% endif %}
{% endfor %}
</div>
{% endif %}
<div class="form-group">
<textarea name="content" id="content" class="form-control" rows="10"></textarea>
</div>
<input type="submit" value="답변등록" class="btn btn-primary">
</form>
</div>
{% endblock %}
question_form.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<h5 class="my-3 border-bottom pb-2">register question</h5>
<form method="post" class="post-form my-3">
{% csrf_token %}
<!-- 오류 표시-->
{% if form.errors %}
<div class="alert alert-danger" role="alert">
{% for field in form %}
{% if filed.errors %}
<strong>{{field.label}}</strong>
{{field.errors}}
{% endif %}
{% endfor %}
</div>
{% endif %}
<!-- ends error-->
<div class="form-group">
<label for="subject">Title</label>
<input type="text" class="form-control" name="subject" id="subject"
value="{{form.content.value|default_if_none:''}}">
</div>
<div class="form-group">
<label for="content" >Content</label>
<textarea class="form-control" name="content" id="content" row="10">
{{form.content.value|default_if_none:''}}
</textarea>
</div>
<!-- {{form.as_p}}-->
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
{% endblock %}
최원호 님 927
M 2021년 3월 11일 7:41 오후
1개의 답변이 있습니다. 1 / 1 Page
우선 views.py 파일에 answer_create 함수가 2개 존재합니다. 따라서 밑에거인 다음 함수가 실행될거구요.
def answer_create(request,question_id):
question=get_object_or_404(Question, pk=question_id)
if request.method =='POST':
form=AnswerForm(request.POST)
if form.is_valid():
answer=form.save(commit=False)
answer.create_date=timezone.now()
answer.save()
answer.author=request.user
return redirect('pybo:detail',question_id=question.id)
else:
form=AnswerForm()
context={'form':form,'question':question}
return render(request,'pybo/question_detail.html', context)
위에 보시면 answer 객체 생성시에 question 속성이 지정이 안되어 있고 save 위치도 잘못되어 있습니다.
다음처럼 수정해 보세요. 아니면 책과 동일하게 입력하시면 됩니다.
def answer_create(request,question_id):
question=get_object_or_404(Question, pk=question_id)
if request.method =='POST':
form=AnswerForm(request.POST)
if form.is_valid():
answer=form.save(commit=False)
answer.author=request.user
answer.question = question
answer.create_date=timezone.now()
answer.save()
return redirect('pybo:detail',question_id=question.id)
else:
form=AnswerForm()
context={'form':form,'question':question}
return render(request,'pybo/question_detail.html', context)
박응용 님
2021년 3월 11일 7:46 오후