Output request parameter by template - django

Is it correct to say that there is no simple tag that just writes some http get
query parameter?
If all needed is printing a http get query parameter e.g. ?q=w
can I directly use the value q with a template tag or need the copy
the value in the request handler?
Is it possible to more directly pass values (all values) from http get
to template?
Because copying every value seems repeating the same handling many
times
template_values = {'q':self.request.get('q'),...
It should be possible to iterate the parameter set. Can you recommend
that or any other solution?

You don't need to do this at all. The request is available in the template context automatically (as long as you enable the request context processor and use a RequestContext) - or you can just pass the request object directly in the context.
And request.GET is a dictionary-like object, so once you have the request you can get the GET values directly in the template:
{{ request.GET.q }}

Related

How to get current url or input parameters in web.py template

I have a web page designed with web.py. I use web.template.render() and an HTML template file to draw a table in this web page.
I pass a list of items to this template, and would like to show the data in different pages using GET method and offset and count parameters (i.e. http://mywebsite/records?offset=10&count=15).
I am looking for a way to get the input values of offset and count and also the current url in the HTML template file, so I would be able to put links to next page and previous page.
Templates get data either passed into them (e.g., render.home(a, b, c)) or as a global (explicitly passed in as a dict via globals parameter when you define your renderer).
Your example is easier to do by getting the information in python, and passing it into the template. This allows you to error check the values, provide defaults, etc.
So, have your GET() extract the proper information and pass it to the template:
class index(object):
def GET(self):
my_render.index(url=web.ctx.fullpath,
offset=web.input().offset,
count=web.input().count())
==== index.html ====
$def with(url, offset, count)
<table>
<tr><td>URL is: $url</td>
<td>OFFSET is: $offset</td>
<td>COUNT is: $count</td></tr>
</table>

django Is there a way to get a reversed url without passing parameters?

I am using ajax data to pass info to my page, but I need to create some links that use data from the javascript to call a view, so the parameters usually passed when one does a {% url 'jobs:close' foo %}, for example, are not available at the time the template is rendered, but imported later through an ajax call. Obviously, this causes errors when the template is rendered.
Is there a way to keep the benefit of having reverse url lookups in such a situation, and dynamically get the URL in the template without passing foo, or do I need to hard code the URL in the template and paste the parameter on the end later?
Instead of getting 'foo' in ajax response, get the reverse url in ajax response and replace the DOM where you need the url.
After thinking about it, and looking at comments, I decided on the answer of adding a fake parameter and removing it afterwards. Since it will be removed when the form is rendered, the parameter doesn't much matter. I chose 1.
So, in my template, I put {% url 'jobs:close' '1' %}. Then in my javascript, as I call the form in a modal, I use jquery to find the form. It makes it easy just to use the same URL again, so here it is:
var modal = $("#modal");
var closeUrl = '{% url 'jobs:close' '1' %}';
closeUrl = closeUrl.substring(0, closeUrl.length - 2) + event.data.id + /;
modal.find("#close-job").attr('action', closeUrl);
event.data.id is where I am storing the job ID that I really need.
It isn't as slick as I was looking for, but it works well.

retrieving action attrubute of form in django view

In my proxy app, I want to retrieve the action of submitted form but I can't find any attribute in request object passed to the view.
I tried request.path but it's incomplete. for example if true action of form was /proxy/?url=http://www.farsnews.com, the value of request.path is /proxy/ that is obviously useless.
I searched the other attributes of request object but didn't help.
The querystring parameters, which are also known as GET parameters, are available via request.GET. This is fully covered in the documentation for the request object.

In the flask-wtf, How to use value of form in the POST process?

i'm new to flask-wtf and i'm encountering problem of Form for POST request.
I have a Form called MyForm which contains fields matched with keys of post parameters.
So, I have initialized Form with post parameter. in the view, it contains right values.
But, when the form is rendering with template, all the value of form is gone.
I can't access values of form in the template. in the way form.field.data.
However, all the value of MyForm can be handled in the template when i process GET method.
it makes me heart, and very annoying! why does value of form can't be handle by template in the POST process?
is that impossible to use value of form in template on POST method?
can anybody help me?
additional information : i pass the value of form to template function.
2nd additional information : OMG, i found that the form is not initialized in the POST process. but in the GET process, it works well.
does form of flask-wtf can not be initialized in POST process?
I have solved it myself.
Form is initialized with formdata parameter which contains json or request.form
usage is like this MyForm(formdata=MultiDict(your json or request.form))

Getting value of URL-encoded request.GET variable in Django Template

I am using Django 1.3 with Python 2.7.2 on Windows 7 x64.
I have a URL pattern like this:
url(r'^(?P<club_id>\d+)/$', 'view_club')
From the pattern I want to be able to use request.GET in my templates to be able to create URLs to my views based on this GET variable (club_id). I have read online that I would be able to do:
{{ request.GET.club_id }}
However, this is always showing up as blank. I cannot figure out the reason why. I have the request context processor in my list of default context processors.
Thanks for the help.
In your example, club_id is not a GET parameter, it is part of the path in the URL. Your view function takes club_id as an argument, it should then put it in the context so the template can access it.
For example, your view function:
def view_club(request, club_id):
return render(request, "template_name.html", { 'club_id': club_id })
then in your template:
{{ club_id }}
IF you are passing an variable in the URL - in this case club_id - simply include it as an argument in your view function next to request:
def get_club(request,club_id):
club = Club.object.get(pk=club_id)
return render_to_response('club.html', {'club': club})
now in your template you can access {{club}} as the individually selected object and do things like {{club.name}} This also works for variables with multiple objects which you would use a for loop to iterate through.