모든 모델에 부착 할 수있는 재사용 가능한 주석 응용 프로그램을 만들었습니다.보기에 문제가 있습니다. 전체 논리를 포함하지 않았으며 질문에만 관련이 있습니다. 더 request.POST이없는 경우Django 1.6 - 재사용 가능한 재전송보기
, 모두가 리디렉션, 나는 점점 오전 오류
dictionary update sequence element #0 has length 0; 2 is required
문제 라인과 몇 가지 문제가 저장된 후 내가 코멘트를 추가하려고하지만 경우에, 잘 작동 context.update (사전)입니다. 덧글을 추가 할 때 비어있는 것처럼 보이지만 그 이유를 모르겠습니다.
내 논리는 이것이다 :
더 request.POST이없는 경우,보기 add_comment이 반환합니다 { 'comment_form'comment_form, '의견'의견을}
request.method==POST'
,context.update(dictionary)
은 반송 방향 재 지정 (노드)으로 인해 을 실행해서는 안됩니다. 보기 프로파일에서 시작 코드의 실행에 이 나타나야합니다. 그 이유는 여기가 리다이렉트 (노드)로 연결되어야하기 때문입니다.
는 난 그냥 profile.views.py에서 재 사용할 수 있습니다 알고 있지만, 나는 매우 unconvenient입니다 추가 의견을 나타낼 수있는 모든보기에 대해 다음을 수행해야합니다.
comment.views.py
from django.shortcuts import redirect
from comment.forms import AddCommentForm
from comment.models import Comment
def add_comment(request, node):
if request.user.is_authenticated():
user = request.user
else:
user = None
comment_form = None
comments = Comment.objects.get_comments(node) # custom manager method for getting all comments
if user:
if request.method == 'POST':
comment_form = AddCommentForm(request.POST)
if comment_form.is_valid():
comment_form.save(node=node, user=user) # custom form save method, updating missing fields
return redirect(node) #redirect to node.get_absolute_url()
else:
comment_form = AddCommentForm()
return {'comment_form': comment_form, 'comments': comments}
profile.views.py - 다른 응용 프로그램, 난 단지 문제는 add_comment
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.models import User
from comment.views import add_comment
def profile(request, id):
user = get_object_or_404(User, id=id)
dictionary = add_comment(request, user)
context = {'user': user}
context.update(dictionary) #problematic line
return render(request, 'profile/profile account.html', context)
매우 빠른 도움을 제공해 주신 데 대해 감사드립니다. 그러나 인스턴스 유형을 확인하는 것이 최후의 수단에서만 사용해야한다고 들었습니다. 아직 프로그래밍에 경험이 없기 때문에 가능할 때마다 좋은 연습을 배워야하는 이유가 있습니다. 그 이유는 재사용 가능한 하위 뷰에 대한 아이디어이므로 항상 피하려고합니다. 중복 된 줄, 이제 첨부 된 주석이있는 모든보기에서 인스턴스 유형을 확인해야합니다. 아마도 add_comment 함수를 counterpart 클래스로 바꿀 수 있습니다. 다시 한 번 도움을 주셔서 감사합니다. – klis87