Search code examples
pythonpython-3.xflaskpythonanywhere

Flask app conflicting routes is it the routes. any ideas?


I am trying to make a forum or code sharing like site on pythonanywhere using a flask app, but I have conflicting routes so it fails.

What this is doing is adding a new line to a .csv file (that already exists) when the user completes and html form I have set up. This works fine on the /addpythoncode route, but it fails on the /addflaskcode route.

I will provide the routes and the error log output below. Can someone please help me fix this?

import sqlite3
from flask import Flask, render_template, request, redirect
import random
import csv

app = Flask(__name__)

@app.route('/addpythoncode', methods=['GET', 'POST'])
def addpycode():
  if request.method == 'GET':
      return render_template('addpythoncode.html')
  else:
      dict = {}
      dict["codename"] = request.form['codename']
      dict["name"] = request.form['name']
      dict["code"] = request.form['code']
      f = open("/home/Ethankbdca/mysite/Pythonforum.csv", "a")
      with f:
          fnames = ['codename', 'name', 'code']
          writer = csv.DictWriter(f, fieldnames=fnames)
          writer.writerow(dict)
          return redirect('http://ethankbdca.pythonanywhere.com/pythonforum')

@app.route('/addflaskcode', methods=['GET', 'POST'])
def newflskcode():
  if request.method == 'GET':
      return render_template('addflaskcode.html')
  else:
      dict = {}
      dict["routename"] = request.form['routecode']
      dict["name"] = request.form['name']
      dict["routecode"] = request.form['routecode']
      f = open("/home/Ethankbdca/mysite/Flaskforum.csv", "a")
      with f:
          fnames = ['routename', 'name', 'routecode']
          writer = csv.DictWriter(f, fieldnames=fnames)
          writer.writerow(dict)
          return redirect('http://ethankbdca.pythonanywhere.com/flaskforum')

Here is the message I get on the error log:

2019-03-26 21:21:43,161: OSError: write error

Solution

  • I agree with Milad M., but I agree with you that there is no such mistake. Change the indentation level of return render_template (...). Add a try-except block to the code where the file is written to.

    import abort 
    
    @app.route('/addpythoncode', methods=['GET', 'POST'])
    def addpycode():
      if request.method == 'GET':
          return render_template('addpythoncode.html')
      else:
          dict = {
              'codename': request.form['codename'], 
              'name': request.form['name'], 
              'code': request.form['code'], 
          }
          try:
              fnames = ['codename', 'name', 'code']
              f = open("/home/Ethankbdca/mysite/Pythonforum.csv", "a")
              writer = csv.DictWriter(f, fieldnames=fnames)
              writer.writerow(dict)
              writer.close()
          except Exception as ex:
              abort(500)
          return redirect('http://ethankbdca.pythonanywhere.com/pythonforum')
    

    This should cause an Internal Server Error on read-write problems.