How to pass context from view to action in html file in django?

只谈情不闲聊 提交于 2019-12-12 03:42:49

问题


I am passing context from view.py file

def change_password(request,pk=None):
    user = MyUser.objects.get(pk=pk)
    if request.method == "POST":
        form = PasswordChangeForm(user=user, data=request.POST)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)  
            return render(request, "nurse/change_password.html", {"user_id":user.id})     // passing user_id as context

I can see this value in my html file

<div class="form-group">
    <label class="control-label allign col-sm-3" > {{user_id}} Old Password </label>    // user_id is giving the right value
</div>

But when i try to fetch the same in action than it is not working:

<form class="form-horizontal login-box2" action="{% url 'booking:change_password' user_id %}" role="form" method="post">   // not getting user_id here
    <div class="form-group">  
        <label class="control-label allign col-sm-3" > {{user_id}} Old Password </label>      // getting user_id here
    </div>
    <div class="form-group"> 
        <li> <button type="submit" class="btn btn-primary f-w-b f-s-18">Change</button> </li>
    </div>
</form>

Can anyone tell me what i am doing wrong?

Thanks

EDITED

adding urls.py code:

url(r'^change_password/(?P<pk>[0-9]+)/$',views.change_password,name='change_password'),

来源:https://stackoverflow.com/questions/37807657/how-to-pass-context-from-view-to-action-in-html-file-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!