apache - Why am getting Bad Request in login page and search form, FLASK? -


if want log control panel getting 400 bad request error says:

bad request

the browser (or proxy) sent request server not understand.

i have search form , if want search through database, getting same error .

the site working on apache webserver, interesting that, if ran application using python manage.py runserver command, error won't show more in apache error showing .

here how code looks in views.py:

@app.route('/login/', methods=('get', 'post'))  @auth.login_required  def login():        if session.get('username') or session.get('is_author') == true:              flash("already logged in .")              return redirect(url_for('index'))        form = loginform()      error = none        if request.method == 'get' , request.args.get('next'):          session['next'] = request.args.get('next')        if form.validate_on_submit():          user = user.query.filter_by(              username = form.username.data          ).first()            if user:              if bcrypt.hashpw(form.password.data, user.password) == user.password:                  session['username'] = form.username.data                  session['is_author'] = user.is_author                    if 'next' in session:                      next = session.get('next')                      session.pop('next')                      return redirect(next)                  else:                      flash('welcome %s'%session['username'].upper())                      return redirect(url_for('index'))                else:                  error = "incorrect username or password ."                  flash(error)          else:              flash(error)              error = "incorrect username or username ."        return render_template('auth/login.html', form=form) 

also search code in views.py:

def replace_last(source_string, replace_what, replace_with):      head, sep, tail = source_string.rpartition(replace_what)      return head + replace_with + tail    @app.route('/search/<query>', methods=['get','post'])  def search_engine(query):        user = user.query.first()        try:          query = request.form['autocomplete']          r = replace_last(query, ' ', '')          posts = post.query.filter(post.title.like('%'+r+'%')).order_by(post.publish_date.desc())        except typeerror:          flash('no results.')          return render_template('main/search_form.html')        context = {              "posts":posts,              "r":r,              "user":user,              "query":query      }        return render_template('main/search_form.html', **context) 

another thing mention that, site running under https , maybe causing problems, don't know !! , in fact using letsencrypt, in article i've seen people saying there's problem when using flask , https when using requests post or get, deleted letsencrypt server restarted apache didn't solved problem , frustrated don't know do.

another thing forgot add, app.conf in /etc/apache2/site-available , maybe way know causing problem :

<virtualhost *:80>                 servername site.com                 serveralias www.site.com                 wsgiscriptalias / /var/www/myapp/flaskapp.wsgi                 <directory /var/www/myapp/siteapp/>                         order allow,deny                         allow                 </directory>                 alias /static /var/www/myapp/siteapp/static                 <directory /var/www/myapp/siteapp/static/>                         order allow,deny                         allow                 </directory>                 errorlog ${apache_log_dir}/error.log                 loglevel warn                 customlog ${apache_log_dir}/access.log combined rewriteengine on rewritecond %{server_name} =www.site.com [or] rewritecond %{server_name} =site.com rewriterule ^ https://%{server_name}%{request_uri} [end,qsa,r=permanent] </virtualhost> 

inside sites-available/app.conf did proxy redirection, ran application using runserver command, inside app.conf added these lines of codes :

    proxypass / http://0.0.0.0:5000/     proxypassreverse / http://0.0.0.0:5000/ 

so that, bad request disappeared, wired thing is, i'am getting wtform error says:

this field required.

means empty, practically not, typing information , handling them inside views.py mentioned above .

eventually, please millions appreciated .

i solved problem, in case faced unbelievable, undebugable problem following:

  1. remove apache2 dependencies, :

    sudo apt-get purge apache2 apache2-utils

  2. then follow tutorial about, how deploy flask application on nginx webserver:

    https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

that's it, i'am happy right :) .


Comments