投稿のモデルを作成しました。その投稿を受け入れるためのフォームを作成する必要があります。以下のビューを使用して、新しいフォームの作成と編集の両方のフォームを生成しています(存在する場合)。
投稿が存在する場合は有効なpost_idがあり、正しい投稿オブジェクトを選択してフィールドに入力したフォームを表示しますが、post_idがない場合は、新しい空白のフォームを生成します。
しかし、私はエラーが発生しています
- post_form()は正確に2つの引数を取ります(1つ指定)
私は何が間違っているのですか?
def post_form(request,post_id):
context_instance=RequestContext(request)
if post_id:
post = get_object_or_404(Post, pk=post_id)
else:
#if the user is authenticated then pass the user object
if request.user.is_authenticated():
post = Post(creator=request.user)
else:
post = Post()
if request.method == 'POST':
if 'save' in request.POST:
form = PostForm(request.POST, instance = post)
if form.is_valid():
form.save()
return redirect(post)
# Instantiate an empty form with the given user
else:
form = PostForm(instance = post)
return render_to_response('forum/post.html', {'form':form}, context_instance)