Can I pass non-URL definition view keyword to a vi

2019-08-23 05:23发布

NoReverseMatch at /natrium/script/4c55be7f74312bfd435e4f672e83f44374a046a6aa08729aad6b0b1ab84a8274/

Reverse for 'run_details' with arguments '()' and keyword arguments '{'script_text': u'print "happy"', 'run_id': '6b2f9127071968c099673254fb3efbaf'}' not found.

This is an excerpt of my views.py

run_id = new_run.run_id
if not run_id:
raise AssertionError("bad run id")

# I tried with args=[run_id, clean['script_text']] too
return HttpResponseRedirect(reverse('run_details', kwargs={'run_id':run_id, 'script_text':clean['script_text']})) 

which in turns calling this view function

def run_details(request, run_id, script_text):
    """
    Displays the details of a given run.
    """
    run = Run(run_id)
    run.update(request.user)

    codebundle = CodeBundle(run.cbid)
    codebundle.update(request.user)

    return render_response(request, "graphyte/runs/run_script.html",
                           {'run':run, 'codebundle':codebundle, 'files':run.artifacts, 'bundle':codebundle,
                            'source_code': script_text
                           })

Now this is my urls.py. The actual redirect views is in another app (kinda insane, but whatever...).

urlpatterns = patterns("webclient.apps.codebundles.views",
    # many.....
    url(r"^cb/newfolder/$", 'codebundle_newfolder', name="codebundle_newfolder"),
)

urlpatterns += patterns('webclient.apps.runs.views',
        url(r"^run_details/(?P<run_id>\w+)/$", 'run_details', name="run_details"),)

This is getting really nasty for the last three hours. I am not sure what's going on. Can someone help me debug this?

Thanks.


The original plan did not have script_text, and I used args=['run_id'] only. It works. In other words, remove script_text from the two views everything will work.


EDIT Sorry for the confusion. Script text is just a context variable that I need to pass to the reverse destination, and from there I render my template. The URLs should only display the run_id.

2条回答
Fickle 薄情
2楼-- · 2019-08-23 06:04

No, you can't really pass an 'extra keyword' to the view function when redirecting. I'll try to explain why.

When you return HttpResponseRedirect, Django returns a response with a 302 status code, and the new location.

HTTP/1.1 302 Found
Location: http://www.example.com/new-url/

Your browser will then usually fetch the new url, but that's a separate request. If your view needs a keyword, it needs to be included in that response somehow, unless you store state in the session. Your two options are

  1. Include the extra keyword in the url:

    http://www.example.com/new-url/keyword-value/

  2. Include the extra keyword as a GET parameter

    http://www.example.com/new-url/?keyword=keyword-value.

    Then in your view, grab the keyword with keyword=request.GET['keyword']. Note that the keyword is no longer a kwarg in the view signature.

A third approach is to stick the keyword into the session before you redirect, then grab it out the session in the redirected view. I would advise against doing this because it's stateful and can cause odd results when users refresh pages etc.

查看更多
姐就是有狂的资本
3楼-- · 2019-08-23 06:07

Your run_details url doesn't accept a kwarg named script_text at all -- remove that from your reverse kwargs.

查看更多
登录 后发表回答