Reference: How do I construct a Django reverse/url using query args?
This is in somepage.html
<a href={% query_urls from={{from}} to={{to}} %}> LOL LOSER</a>
First go to somepage
views, and then clicking whatever button will redirect to move
views.
def move(request):
to = request.GET.get('to', 'None')
ffrom = request.GET.get('from', 'None')
#raise AssertionError(ffrom)
return render_to_response(request, "move.html", {'to': to, 'from': ffrom})
def somepage(request):
to = '../mydir'
ffrom = './heere.py'
return render_to_response(request, "somepage.html", {'to': to, 'from': ffrom})
Instead of getting something like
http://localhost/web/move?from=./here.py&to=../mydir
I get this
http://localhost/web/move?from={{from}}&to={{to}}
Those context vars didn't get render at all, probably because the custom tag(applied to somepage
views) takes all the parameters as string. How do I force to render it first?
Thanks.
** EDIT ** Small question: If I want to achieve this
url(r"^search/<?P(cbid)\d+>/", 'views.search', name='search')
I get Malformed arguments to query_urls tag
if I put this in template
<a href={% query_url 'search' 12456 from=from to=to %}> MY LINK </a>
What's the generic way of writing my custom tag to allow this?
Currently, this is what I do... which works...
def render(self, context):
view_name = self.view_name.resolve(context)
kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context))
for k, v in self.kwargs.items()])
cbid = kwargs['cbid']
kwargs = sorted(kwargs.items(), key=lambda x:x[0]) # sorted and generate a list of 2-tuple
# kwargs query set now contains no cbid
kwargs = [ value for index, value in enumerate(kwargs) if value[0] != 'cbid']
#raise AssertionError(urllib.urlencode(kwargs))
return (reverse(view_name, args=[(cbid),], current_app=context.current_app)
+ '?' + urllib.urlencode(kwargs))
I want to make it more generic, to match any pattern, not just cbid.
<a href={% query_url 'search' cbid=12456 from=from to=to %}> MY LINK </a>
A dumb way (and probably the only way) is to write something like this in the template
{% query_url 'view_func' args=[(cbid, some_text, more_text,)], from=foo to=bar %}
where args is taken literally as a list of args just as in a regular python function. We can probably eval
this into a list instead of a literal string.