How to pass in multiple parameters to web.py's render - python

I am using Jinja2 with web.py and have come across what seems to be a simple problem. I am rendering my paramertized html files and can't seem to figure out how to pass in multiple parameter=value pairs without typing each on in as arguments. I tried passing in a dict and list of strings with no success.
If I want to render home.html which has five parameters that need values, how can I pass in their values without having to type param1=value1, param2=value2 as arguments to the reder.home() function?
I was hoping something like this would work:
from web.contrib.template import render_jinja
render = render_jinja('templates', encoding = 'utf-8',)
p = {}
p['param1'] = 56
p['param2'] = 'something'
...
render.home(p)
PS. the web.py template examples seem to only cover the single param example.

You can use dictionary expansion, like so
render.home(**p)

Related

QueryString in Django

I need one small help. I have one django application already with me and everything is working fine. Now requirement is without making change in existing functions we want to pass urls parameter as a query string
path('my-app/', include('myapp.urls', namespace='myapp')),
# I added this line because I want to use dcid as a querystring
url(r'^my-app/(?:(?P<dcid>\w+)/)',include('myapp.urls', namespace='myapp')),
and my function is already written like this
def orders_b2b_open_list_pageable(request):
We don't want to change in above functions but we want dcid in query string. How can we achieve that
I am doing this but when requesting like this
http://localhost:8002/my-app/1/orders/b2b/open/pageable/
I am getting following error
Thank You in advance
There's no need to parse it in your urls.py, that would change your function signiture.
You can handle it in your view, by pulling the querystring value from the request:
def orders_b2b_open_list_pageable(request):
dcid = request.GET.get('dcid', None)
...

ı wanna post two id from .html to views

html file ı wanna take two id to views.py can ı get it ?
path('yorum_sil/<int:id>',views.yorum_sil,name="yorum_sil")
I want to do this code like the below
path('yorum_sil/<int:comment_id> and <int:post_id>',views.yorum_sil,name="yorum_sil")
That is possible, although you should not use spaces like that. You can for example use a slash like:
path('yorum_sil/<int:comment_id>/<int:post_id>',views.yorum_sil,name="yorum_sil")
Your view function (here yorum_sil) then of course needs to accept the two parameters, like:
# app/views.py
def yorum_sil(request, comment_id, post_id):
# ...
return ...
and if you perform a reverse lookup, you need to pass the two parameters. For example in a template like:
some_link
If I translated yorum sil correctly it means delete comments, note that typically GET requests should not have side effects. In order to delete/create/... an entity, you should use a POST request.

Python elasticsearch-dsl sorting with multiple fields

I'm trying to form the command for sorting using elasticsearch-dsl. However I have trouble passing the variable in correct format in.
The format should be
s=Search()
s = s.sort({"time":{"order":"asc"}}, {"anoter_field":{"order":"desc"}})
s.execute()
The problem is I'm trying to put {"time":{"order":"asc"}}, {"anoter_field":{"order":"desc"}} as a variable, but I can't seem to get this in the right syntax. I tried using dict, list, and string, and none seems to work.
My input would be a dict that looks like
input = {"time":"asc", "another_field":"desc"}
data_input = {"time":"asc", "another_field":"desc"}
args = [{k:{'order':v}} for k,v in data_input.items()]
s.sort(*args)
I guess is what you are asking? Its hard to tell ...

Pyramids route_url with additional query arguments

In Pyramids framework, functions route_path and route_url are used to generate urls from routes configuration. So, if I have route:
config.add_route('idea', 'ideas/{idea}')
I am able to generate the url for it using
request.route_url('idea', idea="great");
However, sometimes I may want to add additional get parameters to generate url like:
idea/great?sort=asc
How to do this?
I have tried
request.route_url('idea', idea='great', sort='asc')
But that didn't work.
You can add additional query arguments to url passing the _query dictionary
request.route_url('idea', idea='great', _query={'sort':'asc'})
If you are using Mako templates, _query={...} won't work; instead you need to do:
${request.route_url('idea', idea='great', _query=(('sort', 'asc'),))}
The tuple of 2-tuples works as a dictionary.

Putting together Haystack records without templates

The haystack documentation (link below) makes this statement:
Additionally, we're providing use_template=True on the text field.
This allows us to use a data template (rather than error prone
concatenation) to build the document the search engine will use in
searching.
How would one go about using concatenation to build the document? I couldn't find an example.
It may have something to do with overriding the prepare method (second link). But in the example given in the documentation the prepare method is used together with a template, so the two might also be orthogonal.
https://github.com/toastdriven/django-haystack/blob/master/docs/tutorial.rst
http://django-haystack.readthedocs.org/en/latest/searchindex_api.html#advanced-data-preparation
You can see how it works in the Haystack source. Basically, the default implementation of the prepare method on SearchField (the base class for Haystack's fields) calls prepare_template if use_template is True.
If you don't want to use a template, you can indeed use concatenation - it's as simple as just joining the data you want together, separated by something (here I've used a newline):
def prepare_myfield(self, obj):
return self.cleaned_data['field1'] + '\n' + self.cleaned_data['field2']
etc.

Categories

Resources