Html form not sending data - python

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.

Related

django: How do i save form in django without the django forms?

I am trying to save a contact form, but i am not using the django form but instead the input field.
I am getting the name, email address and message body from the html templates using input field like this <input type="text" id="name"> but the form doesn't get any data from the input field, it seems i am missing something but i cannot find out what is happening.
I have tried passing in the name, email and message into id="name" id="email" id="message" but nothing still happends.
This is my code
index.html
<form method="POST">
{% csrf_token %}
<input type="text" id="name" value="" />
<input type="text" id="email" value="" />
<input type="text" id="address" value="" />
<button type="button">Let's Withdraw</button>
</form>
views.py
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
message= request.POST.get("message")
print("Message Sent")
return redirect("core:index")
else:
pass
This is your form
<form method="POST">
{% csrf_token %}
<input type="text" id="name" value="" />
<input type="text" id="email" value="" />
<input type="text" id="address" value="" />
<button type="button">Let's Withdraw</button>
</form>
change to this
<form method="POST">
{% csrf_token %}
<input type="text" id="name" name="name" value="" />
<input type="text" id="email" name="email" value="" />
<input type="text" id="address" name="message" value="" />
<button type="submit">Let's Withdraw</button>
</form>
add the name=" ... " attribute in your input field and the corresponding values e.g name, email, address.
Change your button type to submit as your would be submiting a form

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.

Trouble with website login python requests

I have been trying this out for a while now, but can't seem to figure out how to login with requests on this website for a scraping project. Not sure if it might have something to do with the csrfToken..? No matter what I try it doesen't seem to work.
Any help or advice would be greatly appreciated.
Here is the script:
import requests
LOGIN_URL="https://example/user/login"
payload={'login':'username', 'password':'password'}
with requests.Session() as s:
p = s.post(LOGIN_URL, data=payload)
print p.text
r = s.get('https://example.com/account/')
print r.text
here are the html post fields:
<form method="POST" action="https://example.com/user/doConsent" id="consentForm" class="consent-form">
<input type='hidden' name='CsrfToken' value='' />
<input type="hidden" name="key" value="">
<form method="POST" action="https://example.com/user/doLogin" id="loginForm" novalidate>
<input type='hidden' name='CsrfToken' value='' />
<input type="email" class="mb20" name="login" id="login" placeholder="Email or Profile name">
<input type="password" class="mb30" name="password" id="password" placeholder="Password">
If your ‘CsrfToken‘ is empty when you make the actual POST requests, then that's your problem. You need an actual csrf token that matches both the form and the header when you send the data.

make url semantic (dynamic) in Flask

I build a simple flusk service, this is a part of home page:
<form action="/results" method="POST">
<div>
<label class="required">Name: </label>
<input type="text" id="name" name="name" required="required"/>
</div>
<div>
<label class="required">ID: </label>
<input type="text" id="ID" name="ID" required="required"/>
</div>
<button type="submit" class="submit" id="submit" onclick="loading()"> Submit </button>
</form>
Here is a part of python:
#app.route('/', methods=['GET'])
def main_page():
return render_template('ui/home_page.html')
#app.route('/results', methods=['POST'])
def show_results():
# do something...
return render_template('ui/result_page.html')
It works when I click button and shows results in http://localhost:8080/results.
I now want to make the result url to be semantic base on users' input in the home page such as http://localhost:8080/results/user=balabala&ID=balabala. I tried modify the python
#app.route('/results/<url_postfix>', methods=['POST'])
def process_diff_by_platform(url_postfix):
# do something...
return render_template('ui/result_page.html')
but it shows 404 Not Found. I guess this is because that I hard coded the url in html template. But I have no idea how to fix that...
How do I solve this problem?
~ thanks

Submitting a form with onsubmit attribute with 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>

Categories

Resources