im getting a bad request, here is my url code:
#app.route('/login', methods=['POST'])
def login():
print(request)
user = models.User.get(models.User.email**request.form['email'])
if request.form['password'] == user.rpe:
login_user(user)
return jsonify(user.to_json())
here is my html:
<form id="form">
<input type="email" placeholder="email" id="email" name="correo">
<input type="password" placeholder="rpe" id="password" name="password">
<button type="submit" ng-click="submit()">Log in</button>
</form>
and here is my js code:
$scope.submit = function(event){
var form = $('form');
var values = {};
$.each(form.serializeArray(), function(i, field){
values[field.name] = field.value;
});
data = JSON.stringify(values);
console.log(data);
$http.post('/login', values).success(function () {
alert('success');
});
}
after i do the request with the form, here is what i get:
<Request 'http://localhost:8000/login' [POST]>
im guessing that this request isnt what it is suppoused to be, im also asking because in other questions some user added a csrf token but i actually dont know how to add one with javascript since im adding my angular app this way:
#app.route('/')
def main():
return make_response(open('templates/index.html').read())
thanks
Related
I am using parametrized ajax to send data to flask on button click. The data and buttons are in for loops and I want ajax to send single data but it's sending multiple items. The data sent back to flask only repeats for the first dictionary in the list, and is only the value for the first key in the dictionary. The rest generates an error.
Flask code
from Flask import flask,request
app = Flask(__name__)
all_items = [{'name':'sausage','price':10},{'name':'soda','price':50}]
#app.route('/index', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html', all_items = all_items)
Ajax script:
<script type=text/javascript>
$(document).ready(function() {
$('form').on('submit', function(event) {
$.ajax({
data : {
item : $('#item').val(),
price: $('#price').val(),
},
type : 'POST',
url : '/index'
})
.done(function(data) {
//nothing
});
event.preventDefault();
});
});
</script>
Html:
{%for item in all_items%} <!--all_items is a list of dictionaries-->
<form>
<fieldset>
<input type="hidden" name="item" id="item" value="{{item.name}}">
<input type="hidden" name="price" id="price" value="{{item.price}}">
<input type="submit" name="submit" id="submit" value="Add to Cart" class="button" />
</fieldset>
</form>
{% endfor %}
thanks in advance. I know this has been asked a few times. But after reading the previous questions, reading and understanding JSON and AJAX forms tutorials, I still can't find a way to not having the website refreshed after submitting a form. I would really appreciate it if any of you with a higher knowledge of JavaScript is able to give a hand.
This is a newsletter at the bottom part of the Home page that just asks for a name and an email and it keeps the information in the database, it works perfect and I just would like to reply with a confirmation message without refreshing, because it replies a message but the user has to go to the bottom of the page again to see it which is not practical at all.
The HTML is
<div id="contact_form">
<div class="Newsletter">
<form id="form" enctype="multipart/form-data" method="POST" action="" style="text-align: left;">
{% csrf_token %}
<div class="fields">
<div class="fields">
<label for="name" id="name_label">Name</label>
<input type="text" name="name" minlength="3" placeholder="e.g. John Smith" id="name" required>
</div>
<div class="fields">
<label for="email" id="email_label">Email</label>
<input type="email" name="email" placeholder="e.g. john#example.com" id="email" required>
</div>
</div>
<div class="submit">
<button type='submit' id="submit" >Subscribe</button>
</div>
{% include 'messages.html' %}
</form>
</div>
</div>
The index view
def index(request):
"""View function for home page of site."""
if request.method == 'POST':
form = NewsUserForm(request.POST)
if form.is_valid():
instance = form.save(commit=False) #we do not want to save just yet
if NewsUsers.objects.filter(email=instance.email).exists():
messages.warning(request, 'Your email already exists in the newsletter database')
else:
instance.save()
messages.success(request, 'Great! Your email has been submitted to our database.')
try:
send_mail('Welcome ', 'Thank you for subscribing to the Newsletter. ', 'user123#gmail.com',[instance.email], fail_silently=False)
except BadHeaderError: #add this
return HttpResponse('Invalid header found.') #add this
form = NewsUserForm()
return render(request, 'index.html', {'form':form})
Most of the tutorials suggest to create another view + an url for that view + ajax code
I tried this one from here (https://pytutorial.com/how-to-submit-a-form-with-django-and-ajax#top) without success, also eliminating "post" in html method but still not working, even the info got from the form appears in the url. Any help will be welcome, thank you very much.
jquery unsuccessful code
$('#form').on('submit', function(e){
e.preventDefault();
$.ajax({
type : "POST",
url: "{% url 'index' %}",
data: {
name : $('name').val(),
email : $('email').val(),
csrfmiddlewaretoken: '{{ csrf_token }}',
dataType: "json",
},
success: function(data){
$('#output').html(data.msg) /* response message */
},
failure: function() {
}
});
});
unsuccessful ajax view function and url
def ajax_posting(request):
if request.is_ajax():
name = request.POST.get('name', None) # getting data from first_name input
email = request.POST.get('email', None) # getting data from last_name input
if name and email: #cheking if first_name and last_name have value
response = {
'msg':'Your form has been submitted successfully' # response message
}
return JsonResponse(response) # return response as JSON
#path('ajax-posting/', views.ajax_posting, name='index'),# ajax-posting / name = that we will use in ajax url
I created this form:
<html>
<body>
<div>
<form action="{{ url_for('login') }}" method="POST">
<div class="row">
<div>
<input id="email" name="email" type="email" class="validate">
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div>
<input id="password" type="password" name="password" class="validate">
<label for="password">Password</label>
</div>
</div>
<button type="submit" id="login" >Login</button>
<br>
</form>
<div>
</body>
</html>
and I have this Flask app that uses HTTPBasicAuth to do authentication.
#!flask/bin/python
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask import render_template
from flask_httpauth import HTTPBasicAuth
#Needs: pip install flask-httpauth
app = Flask(__name__)
auth = HTTPBasicAuth()
#app.route('/', methods=['GET','POST'])
#auth.login_required
def login():
print('in login')
print(request.values.get('email'), request.values.get('password'))
templateToReturn = 'login.html'
if request.method == 'POST':
print('in post')
username = request.values.get('email')
password = request.values.get('password')
if verify_password(username, password):
print('password verified')
templateToReturn = 'index.html'
print('Curr user', auth.current_user())
print('request: ', request.method)
if request.method == 'GET' and auth.current_user():
templateToReturn = 'index.html'
return render_template(templateToReturn)
#app.route('/logout')
def logout():
return render_template('logout.html')
#auth.verify_password
def verify_password(email, password):
print('in verify pwd')
return verifyAuthentication(email, password)
def verifyAuthentication(email, password):
knownUsers = {'p1#gmail.com': 'pass',
'p2#yahoo.com': 'pass'}
authenticated = False
if email in knownUsers:
if knownUsers[email] == password:
authenticated = True
return authenticated
When I click the submit button of the form, I'm taken to the login() function. But isn't there supposed to be some way that it should go to the verify_password() function because it's decorated with #auth.verify_password?
How exactly and in which part of the code does the user authentication get registered with Flask? By which I mean: When does the #auth.login_required decorator actually allow their corresponding decorated functions to get executed?
Even the official page of HTTPBasicAuth() didn't explain this with an HTML example. Could someone please explain by adding to my code.
You forgot to add name attribute in your HTML input tag, so ideally it should be -
<input id="email" name="email" type="email" class="validate" />
<input id="password" name="password" type="password" class="validate" />
I have a form. When I submit my form, I want to execute some functions in the route that processes the form data.
Form
<form action="{{ url_for('background_process') }}" method="POST">
<input type="text" id="name" name="name" required />
<input type="url" id="url" name="url"/>
<input type ="text" id="mdp-demo" name= "mdp-demo"/>
<input type=text size=5 name=proglang>
<a href=# id=process_input><button class='btn btn-default'>Submit</button></a>
</form>
Javascript to post form data to route that processes data.
$(function() {
$('a#process_input').bind('click', function() {
$.getJSON('/background_process', {
proglang: $('input[name="proglang"]').val(),
url: $('input[name="url"]').val(),
title: $('input[name="name"]').val(),
dates: $('input[name="mdp-demo"]').val(),
}, function(data) {
$("#result").text(data.result);
});
return false;
});
Route that processes the form data.
#app.route('/background_process', methods=['GET','POST'])
def background_process():
try:
create_table()
data_entry()
url = request.args.get('url', 0, type=str)
title = request.args.get('title', 0, type=str)
dates = request.args.get('dates', 0, type=str)
if url.lower() == 'https://www.reddit.com/':
return jsonify(result='You are wise')
else:
return jsonify(result='Try again.')
except Exception as e:
return str(e)
I want to execute the create_table() and data_entry() functions when the submit button on the form is clicked.
The result at the moment is that nothing happens. These functions work when executed on app run time outside of a route. It's when they are within a route they don't work. Any suggestions as to what I should do to achieve execution of these functions inside my route?
I can't get my login def to work in my python/flask code
I have followed multiple tutorials on the internet but none of them works for me. The page does load the login screen but after that, it throws an error:
Bad Request The browser (or proxy) sent a request that this server could not understand.
When trying to switch things up and use different solutions the page keeps showing me the login page no matter what credentials you enter.
#app.route('/login', methods=['GET', 'POST'])
def login_user():
if request.methods == "POST":
if request.form['password'] == 'password' and request.form['username'] == 'username':
session['logged_in'] = True
return redirect(url_for('hello_world'))
else:
flash('wrong')
return render_template("voorbeeld.html")
#app.route('/', methods=['GET', 'POST'])
def hello_world():
my_var = session.get('logged_in', None)
if my_var != True:
return render_template("voorbeeld.html")
else:
remainder of my code
<!DOCTYPE html>
<head>
<title>your title here</title>
</head>
<body>
<form action="/login" method="POST">
<input type="username" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="log in">
</form>
</body>
</html>
I would like the login page to send the user back to the def hello_world and then follow the remainder of the code.