retrieving action attrubute of form in django view - django

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.

Related

Can I generate a form depending on the results of a view?

I need to create a form which depends on the results of a view. I can generate it within the view and/or create a view template in order to do this....But how do I process it?
How do I connect my form/template generated form to a form_submission function?
When this custom/dynamic form is submitted I need to call some drupal functions to create some content in the site.
I did not find any way to do it in a view, but what I did was:
- Create a form programatically, making only a hidden field for NIDS
- Place my view next to that form using AJAX
- When the view is updated I copy the results of the view to the NIDS field and submit the form (This reloads the page)
- Form submission has 2 behaviours, 1 to re-generate the form when provided with NIDs, and other one for the actual form created by the NIDS result of my view.
Just in case this helps someone

Back button on Django

I am trying to use HTTP_REFERER to create a link to the previous page. However, let's say I have a 'page1' that calls the 'page2' get method. It will show a form at the 'page2' where I can do a post request. When I post on the 'page2', the HTTP_REFERER is referencing the same page (page2), therefore I can't to back to the 'page1'. Is there anything I can do to go back to the 'page1'?
You will need to add a hidden next field to your form . This will capture the previous page by making use of the HTTP_REFERER. After your post request you can then perform a redirect to the page you were on before the form page.
Edit:
More information can also be found here.

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))

Propagating next parameter in login in oauth2app

I have a Django Web site where I'm using the oauth2app library for OAuth 2 authentication. I've also modified oauth2app to use crispy_forms, instead of the deprecated uni_form.
The authorize() method in oauth2app is guarded by a Django decorator, #login_required. Indeed, if a client tries to authorize without first logging in, the login page appears.
As expected, the decorator causes the login page URL to have a "next" CGI parameter with the original URL for the authorization request.
The problem is, the "next" parameter is not propagated when the template for the login page is instantiated. What I'd like is for the form "action" to contain the "next" parameter.
This page purports to offer a solution:
http://django-uni-form.readthedocs.org/en/latest/helpers.html
in the section "Manipulating a helper in a view".
Following that example, I tried:
form = LoginForm()
redirect_url = request.GET.get('next')
if redirect_url is not None:
form.helper.form_action = reverse(login) + '?next=' +
redirect_url
But the source for the resulting page shows the original "action" for the form. The assignment to the form_action appears not to take effect. Indeed, if I don't instantiate the template, but just return the form_action, it hasn't changed:
return HttpResponse('action: ' + form.helper.form_action)
Color me puzzled.
The author of uni_forms clued me in here.
The problem is that the form helper was a property, so that the form_action was a fixed piece of that property.
By changing the property to a method that creates the helper, the form_action can be dynamically modified.
Everything works now, no Javascript hacks needed.

Output request parameter by template

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 }}