1

我已经为帖子制作了模型,并且必须制作表格来接受该帖子。我正在使用下面给定的视图来生成一个表单,用于制作新表单和编辑表单(如果存在)。

如果帖子存在,那么将有一个有效的 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)
4

1 回答 1

1

看起来你需要一个默认值post_id

def post_form(request, post_id=None):
    ...
于 2011-11-12T17:59:19.743 回答