开发者

Value from one view's form to other view

开发者 https://www.devze.com 2023-01-06 08:44 出处:网络
Can I perform in django such operation, that in one view I assign a value of return from other view, that renders its own form template and basing on it returns value ? (So in other words if on this f

Can I perform in django such operation, that in one view I assign a value of return from other view, that renders its own form template and basing on it returns value ? (So in other words if on this form rendere开发者_高级运维d by the other function user clicks ok, I return true, and if user clicks cancel I return false) Sample code :

Main function:

def my_func(request):
    result = False
    result = redirect('some_url')

    if result:
        redirect somewhere
    else:
        redirect somewhere else

Child function called from parent with 'some_url' :

def some_url_func(request):
     if request.POST.get("action") == "ok":   
        return True
     elif if request.POST.get("action") == "cancel":
        return False    

Form:

<form action="some_url_func" method="post">
    <input type="submit" class="submit" name="submit" action="ok" value="Ok" />
    <input type="submit" class="submit" name="submit" action="cancel" value="Cancel" />
</form>

So basically is it possible to go through one view to some form and then return to this view ?


As mentioned above your first function makes no sense; but I think your main idea is that you can just call another function with your request and look at the returned value; but you shouldn't call it a view function if it doesn't return a HttpResponse object, it's just some sort of helper function. I think what you wanted to do is:

def my_func(request):
    result = some_url_func(request)
    if result:
        redirect somewhere
    else:
        redirect somewhere else

But maybe you should put your second function then in a module like utils since its more a helper function than a view!

0

精彩评论

暂无评论...
验证码 换一张
取 消