No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

32 líneas
1.1KB

  1. from flask import Blueprint, render_template, request, flash, redirect, url_for
  2. from flask_login import login_required, current_user
  3. from .models import Tool
  4. from . import db
  5. tool = Blueprint('tool', __name__)
  6. @tool.route('/create', methods=('GET', 'POST'))
  7. @login_required
  8. def create():
  9. if request.method == 'POST':
  10. name = request.form.get('name')
  11. description = request.form.get('description')
  12. if not name:
  13. flash('Name is required!')
  14. else:
  15. tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database
  16. if tool: # if a tool is found, we want to redirect back to create page
  17. flash('Tool with same name already exists')
  18. return redirect(url_for('tool.create'))
  19. # create a new tool with the form data
  20. new_tool = Tool(name=name, description=description)
  21. # add the new user to the database
  22. db.session.add(new_tool)
  23. db.session.commit()
  24. return render_template('create.html')