如何将表单操作中的参数传递给视图? Django的

时间:2017-07-09 18:06:12

标签: django python-3.x django-forms django-templates django-views

这是我的视图功能:需要一个kwarg,即block_id

def write_comment(request, block_id):
    block=get_object_or_404(Block, pk=block_id)
    if request.method=='POST':
        form=Comment_form(request.POST)
        if form.is_valid():
            #do stuff
        else:
            return render(request,'writers_block/index.html', {'comment_form':form, 'block_form':Block_form()})

要从我的模板表单访问它,我使用:

{%for block in block_list%}
<form method="post" action="{%url 'write_comment' block_id=block.id%}">
{%csrf_token%}
{{comment_form}}
<input type="submit" value="submit">
</form>
{%endfor%}

不知怎的,我总是得到一个NoReverseMatch,说没有找到带有那些参数的write_comment:

urlpattern是:

url(r'^write_comment', writers_block.views.write_comment, name='write_comment')

你能帮助我吗?

1 个答案:

答案 0 :(得分:2)

你可以像这样更改你的网址并尝试..        import tweepy # Authenticate connection auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(token_key, token_secret) # Start API api = tweepy.API(auth) # Update status api.update_status('List of fruits: \n1. Apple \n2. Banana \n3. Orange')