Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

32 rindas
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')