카테고리 만드는 중 질문 등록하기를 누르면 question_create() missing 1 required positional argument: 'category_name'이 뜸니다
안녕하세요 지금 자유게시판 카테고리를 만들고있습니다. 질문과답변, 자유게시판 두개의 카테고리를 만든 후 질문 등록하기 버튼을 누르면 question_create() missing 1 required positional argument: 'category_name'이와 같은 오류가 계속 발생합니다. 코드가 오류인지 계속 찾아 보았지만 도무지 모르겠습니다. 저런 오류는 어떻게 해결하나요? 도와주세요
저자님의pybo.kr과 https://velog.io/@azzurri21/Django-점프투장고-카테고리-기능-추가 을 참고하였습니다. 아래는 코드입니다.
models.py
class Category(models.Model):
name = models.CharField(max_length=20, unique=True)
description = models.CharField(max_length=200, null=True, blank=True)
has_answer = models.BooleanField(default=True) # 답변가능 여부
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('pybo:index', args=[self.name])
class Question(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='author_question')
subject = models.CharField(max_length=200)
modify_date = models.DateTimeField(null=True, blank=True)
content = models.TextField()
create_date = models.DateTimeField()
voter = models.ManyToManyField(User, related_name='voter_question') # 추천인 추가
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='category_question', null=True)
def __str__(self):
return self.subject
question_views.py
urls.py
urlpatterns = [
# base_views.py
path('',
base_views.index, name='index'),
path('question/list/
base_views.index, name='index'),
path('
base_views.detail, name='detail'),
# question_views.py
path('question/create/<str:category_name>/',
question_views.question_create, name='question_create'),
path('question/create/',
question_views.question_create, name='question_create'),
path('question/modify/<int:question_id>/',
question_views.question_modify, name='question_modify'),
path('question/delete/<int:question_id>/',
question_views.question_delete, name='question_delete'),
base_views.py
def index(request, category_name='qna'):
"""
pybo 목록 출력
"""
# 입력 인자
page = request.GET.get('page', '1') # 페이지
kw = request.GET.get('kw', '') # 검색어
so = request.GET.get('so', 'recent') # 정렬기준
category_list = Category.objects.all()
category = get_object_or_404(Category, name=category_name)
question_list = Question.objects.filter(category=category)
# 정렬
if so == 'recommend':
question_list = Question.objects.annotate(num_voter=Count('voter')).order_by('-num_voter', '-create_date')
elif so == 'popular':
question_list = Question.objects.annotate(num_answer=Count('answer')).order_by('-num_answer', '-create_date')
else: # recent
question_list = Question.objects.order_by('-create_date')
# 조회
#question_list = Question.objects.order_by('-create_date')
if kw:
question_list = question_list.filter(
Q(subject__icontains=kw) | # 제목검색
Q(content__icontains=kw) | # 내용검색
Q(author__username__icontains=kw) | # 질문 글쓴이검색
Q(answer__author__username__icontains=kw) # 답변 글쓴이검색
).distinct()
# 페이징 처리
paginator = Paginator(question_list, 10) # 페이지당 10개씩 보여 주기
page_obj = paginator.get_page(page)
context = {'question_list': page_obj, 'page': page, 'kw': kw, 'so': so,
'category_list': category_list, 'category': category} #page와 kw가 추가됨
return render(request, 'pybo/question_list.html', context)
@login_required(login_url='common:login')
def question_create(request, category_name):
"""
pybo 질문 등록
"""
category = Category.objects.get(name=category_name)
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.category = category
question.save()
return redirect('pybo:index')
else:
form = QuestionForm()
context = {'form': form, 'category': category}
return render(request, 'pybo/question_form.html', context)
@login_required(login_url='common:login')
def question_modify(request, question_id):
"""
pybo 질문 수정
"""
question = get_object_or_404(Question, pk=question_id)
if request.user != question.author:
messages.error(request, '수정권한이 없습니다.')
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.author = 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, 'category': question.category}
return render(request, 'pybo/question_form.html', context)
tsp3546 님 514
M 2022년 6월 22일 6:54 오전