답변을 달면 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 %}

최원호 482

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 오후

선생님이 알려주신데로 수정했습니다. 그런데 동일한 IntegrityError가 발생합니다. IntegrityError at /pybo/answer/create/191/ NOT NULL constraint failed: pybo_answer.auther_id Request Method: POST Request URL: http://127.0.0.1:8000/pybo/answer/create/191/ Django Version: 3.1.3 Exception Type: IntegrityError Exception Value: NOT NULL constraint failed: pybo_answer.auther_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: Mon, 15 Mar 2021 00:50:01 +0000 아래는 수정한 pybo/views.py 코드입니다. 무엇인 문제인지 파악이 안됩니다. from django.shortcuts import render, get_object_or_404, redirect # Create your views here. 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 from django.contrib.auth.decorators import login_required 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 HttpResponse(" Welcome to Pybo") 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) @login_required(login_url='common:login') 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) @login_required(login_url='common:login') 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) # question.answer_set.create(content=request.POST.get('content'),create_date=timezone.now()) 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월 15일 9:55 오전 추천 , 대댓글
@최원호님 answer_create 함수에도 @login_required(login_url='common:login') 애너테이션을 넣어 보세요. - 박응용님, 2021년 3월 15일 10:55 오전 추천 , 대댓글
@박응용님 @annotation을 아래에도 붙이고 글쓴이 표시와 답변에 추가기능을 시행했더니 동일한 에러가 납니다. 혹시나 해서 파이참을 끄고 다시 시작해 보았지만 역시 동일한 에러가....갈팡지팡... IntegrityError at /pybo/answer/create/192/ NOT NULL constraint failed: pybo_answer.auther_id Request Method: POST Request URL: http://127.0.0.1:8000/pybo/answer/create/192/ Django Version: 3.1.3 Exception Type: IntegrityError Exception Value: NOT NULL constraint failed: pybo_answer.auther_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: Wed, 17 Mar 2021 06:47:14 +0000 #pybo/question_list.html {% extends 'base.html' %} {% load pybo_filter %} {% block content %} <div class="container my-3"> <table class="table"> <thead> <tr class="text-center thead-dark"> <th>번호</th> <th style="width:50%">제목</th> <th>글쓴이</th> <th>작성일시</th> </tr> </thead> <tbody> {% if question_list %} {% for question in question_list %} <tr class="text-center"> <!-- <td>{{ forloop.counter }}</td>--> <td>{{ question_list.paginator.count|sub:question_list.start_index|sub:forloop.counter0|add:1 }} </td> <td class="text-center"> <a href="{% url 'pybo:detail' question.id %}">{{ question.subject }} {% if question.answer_set.count > 0 %} <span class="text-danger small ml-2"> {{ question.answer_set.count }} </span> {% endif %} </a> </td> <td>{{question.author.username}}</td> <td>{{ question.create_date }}</td> </tr> {% endfor %} {% else %} <tr> <td colspan="3">질문이 없습니다.</td> </tr> {% endif %} </tbody> </table> <!-- 페이징처리 시작 --> <ul class="pagination justify-content-center"> <!-- 이전페이지 --> {% if question_list.has_previous %} <li class="page-item"> <a class="page-link" href="?page={{ question_list.previous_page_number }}">이전</a> </li> {% else %} <li class="page-item disabled"> <a class="page-link" tabindex="-1" aria-disabled="true" href="#">이전</a> </li> {% endif %} <!-- 페이지리스트 --> {% for page_number in question_list.paginator.page_range %} {% if page_number >= question_list.number|add:-5 and page_number <= question_list.number|add:5 %} {% if page_number == question_list.number %} <li class="page-item active" aria-current="page"> <a class="page-link" href="?page={{ page_number }}">{{ page_number }}</a> </li> {% else %} <li class="page-item"> <a class="page-link" href="?page={{ page_number }}">{{ page_number }}</a> </li> {% endif %} {% endif %} {% endfor %} <!-- 다음페이지 --> {% if question_list.has_next %} <li class="page-item"> <a class="page-link" href="?page={{ question_list.next_page_number }}">다음</a> </li> {% else %} <li class="page-item disabled"> <a class="page-link" tabindex="-1" aria-disabled="true" href="#">다음</a> </li> {% endif %} </ul> <!-- 페이징처리 끝 --> <a href="{% url 'pybo:question_create' %}" class="btn btn-primary">질문 등록하기</a> </div> {% endblock %} #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 text-left"> <div class="mb-2">{{question.author.username}}</div> <div>{{ question.create_date }}</div> </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 text-left"> <div class="mb-2">{{answer.author.username}}</div> <div>{{ answer.create_date }}</div> </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" {% if not user.is_authenticated %} disabled{% endif %} id="content" class="form-control" rows="10"></textarea> </div> <input type="submit" value="답변등록" class="btn btn-primary"> </form> </div> {% endblock %} #pybo/views.py from django.shortcuts import render, get_object_or_404, redirect # Create your views here. 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 from django.contrib.auth.decorators import login_required 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 HttpResponse(" Welcome to Pybo") 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) @login_required(login_url='common:login') 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) @login_required(login_url='common:login') 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) @login_required(login_url='common:login') def answer_create(request,question_id): question=get_object_or_404(Question, pk=question_id) # question.answer_set.create(content=request.POST.get('content'),create_date=timezone.now()) 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월 17일 3:56 오후 추천 , 대댓글