Submitting a form with onsubmit attribute with python - python

I am trying to submit a form through python and I send a urllib request with a url like
https://example.com/form_submitted?element_1=data
It worked on another form but this one contains onsubmit attribute in the form tag and it only redirects me to the form page with the input field filled with data and if I click the submit button, it works.
I also tried https://example.com/form_submitted?element_1=data&submit=Submit but it didn't work. What can I do?
<form id="form_1" method="post" action="form_submitted" onsubmit>
<label for="element_1">Text</label>
<div>
<input id="element_1" name="element_1" type="text" maxlength="255" value=""/>
</div>
<input id="saveForm" type="submit" name="submit" value="Submit" />
</form>

Related

Reset Password Using Django

I am trying to reset password using Django but I am getting the following error:
Method Not Allowed (POST): /reset/done/
Method Not Allowed: /reset/done/
Below are my forms:
Form Which Sent Link To My Email
<form action="{% url 'reset_pass' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required autofocus>
</div>
<input type="submit" value="Send Email" class="btn btn-primary btn-block">
</form>
Form Which I Get On Clicking That Link
<form action="{% url 'password_reset_complete' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input type="password" class="form-control" name="new_pass" placeholder="New Password" required autofocus>
</div>
<div class="form-group">
<input type="password" class="form-control" name="confirm_pass" placeholder="Confirm Password" required>
</div>
<input type="submit" value="Update Password" class="btn btn-primary btn-block">
</form>
URL
path('reset/pass/', views.reset_pass, name='reset_pass'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="feebeeapp/reset_form.html"), name='password_reset_form'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(template_name='feebeeapp/login.html'), name='password_reset_complete')
Not sure, what I am doing wrong. Can someone please guide me?
Your form is submitting to the wrong view. You are supposed to make a POST request to a PasswordResetConfirmView view.
If your PasswordResetConfirmView is thus registered as:
path('reset/', auth_views.PasswordResetView.as_view(), name='password_reset')
then in your form you work with:
<form action="{% url 'password_reset' %}" method="post">
…
</form>
Normally you already use this view to render the form. So you submit a POST request to the same view.
This view will send an email with a reset link, and then will redirect to the password_reset_complete view.

How to Input type text with formaction in django?

In HTML can input type text have a formaction="" attribute?
I mean a submit button can have a separate formaction like: Reference
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formaction="demo_admin.asp" value="Submit as admin">
</form>
So can i use formaction attribute with type="text"? Like:
<form action="{% url 'packOrders' %}" method="POST">
{% csrf_token %}
<input type="text" name="lname" formaction="{% url 'login' %}">
</form>
What I am trying to do is to make box with text input so that when i input some text and press enter, it should not call the main form's action. Instead should pass the value to a different action defined in formaction.
This is similar like my other question but different

Django form post not getting values in request.GET

I am trying to pass a value to my view using a form as follows,
I have the following form:
<form action="{% url 'searchlocation' %}" method="get">
{% csrf_token %}
<input type="text" class="form-control" id="search_location" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-danger"><i class="fa fa-search"></i>
</button>
</span>
</form>
and in my view i have:
def SearchLocation(request):
if request.method == 'GET':
keyword = request.GET.get('search_location','')
print keyword
return render_to_response('app/location.html', {'user': request.user}, RequestContext(request))
I am not getting any value for keyword here. It just gives me a ''.
When i tried request.GET['search_location'], it gave me a multivaluedict error.
You must supply a name for your form input in order for it to be sent by the browser, like so:
<input type="text" name="search_location" class="form-control" id="search_location" placeholder="Search">
Request data is sent in the form of a key/value pair. With no name attribute, there is no key for the value and the input is not sent by the browser.

Html form not sending data

I have a form action but it's not sending data and I don't know why, so I was hoping you could tell me why.
Here's my form:
<form method="post" action="http://myapp.herokuapp.com/test/url">
<input placeholder="Name" type="text" name="user" maxlength="30">
<input placeholder="Password" type="password" name="password">
<input placeholder="email" type="text" name="mail">
<input class="btn btn-register" type="submit" value="¡Register now!" />
</form>
And here's my back-end code (Python):
def test_1(request):
return HttpResponse(simplejson.dumps({'post': request.POST, 'get': request.GET}))
What I get:
{"get": {}, "post": {}}
I have already an app working fine, and now I'm doing a Phonegap version of it.
Any ideas?
Edit:
When I change the url for http://myapp.herokuapp.com/test/url/ I get this:
CSRF verification failed. Request aborted.

How can I access the current URI in python's mako templating system?

I'd like to submit the form to the current URI, like this:
<form action="${CURRENT_URI}" method="post">
<input type="text" name="email" />
</form>
from within a mako template. But I am not sure what variable holds the current uri information.
Thanks.
Actually, you don't need it.
<form action="" method="post">
<input type="text" name="email" />
</form>
If you leave the action empty, it will be posted to the current url.
However, if you need the current url for some other reason, it can be retrieved by calling pylons.url.current()

Categories

Resources