@annotation을 아래에도 붙이고 글쓴이 표시와 답변에 추가기능을 시행했더니 IntegrityError가 납니다.

@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)

최원호 377

M 2021년 3월 19일 5:44 오후

목록으로
2개의 답변이 있습니다. 1 / 1 Page

작성하신 모델과 로그인 세션 처리하는 부분을 보여주세요.
그리고 작성하신 소스말고 책에서 제공되는 깃허브 소스로 해도 마찬가지인지 확인해 보시기 바랍니다.

박응용

2021년 3월 19일 5:48 오후

관심을 가져주셔서 감사합니다.
아래에 모델과 뷰, 로그인 html의 코드가 있습니다.

model.py

from django.db import models

from django.contrib.auth.models import User

Create your models here.

class Question(models.Model):
author=models.ForeignKey(User, on_delete=models.CASCADE, null=True)
subject=models.CharField(max_length=200)
content=models.TextField()
create_date = models.DateTimeField()
modify_date=models.DateTimeField(null=True,blank=True)

class Answer(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
question=models.ForeignKey(Question, on_delete=models.CASCADE)
content=models.TextField()
create_date = models.DateTimeField()
modify_date = models.DateTimeField(null=True, blank=True)

pybo/templates/login.html



Title


{% extends 'base.html' %}
{% block content %}


{% csrf_token %} {% include "form_errors.html" %}


{% 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

from django.contrib import messages

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):
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)
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={'question':question,'form':form}
return render(request,'pybo/question_detail.html', context)

@login_required(login_url='common:login')
def question_modify(request,question_id):
question=get_object_or_404(Question,pk=question_id)
if request.user !=question.auther:
messages.error(request,'You have no permission')
return redirect('pybo:detail',question_id=question.id)

if request.method=="POST":
    form=QuestionForm(request.POST,instance=question)
    if form.is_valid():
        question=form.save(commit=False)
        question.auther=request.user
        question.modify_date=timezone.now()
        question.save()
        return redirect('pybo:detail',question_id=question.id)
else:
    form=QuestionForm(instance=question)
    context={'form':form}
    return render(request,'pybo/question_form.html',context)

@login_required(login_url='common:login')
def question_delete(request,question_id):
question=get_object_or_404(Question,pk=question_id)
if request.user !=question.author:
messages.error(request,'have no permission to delete')
return redirect('pybo:detail',question_id=question.id)
question.delete()
return redirect('pybo:index')

최원호

2021년 3월 23일 11:36 오전