# Buggy code
def trigger(self, session=None):
    dag_id = request.values.get("dag_id")
    origin = request.values.get("origin") or "/admin/"

    if request.method == "GET":
        return self.render(
            "airflow/trigger.html", dag_id=dag_id, origin=origin, conf=""
        )

    dag = (
        session.query(models.DagModel).filter(models.DagModel.dag_id == dag_id).first()
    )
    if not dag:
        flash("Cannot find dag {}".format(dag_id))
        return redirect(origin)

    execution_date = timezone.utcnow()
    run_id = "manual__{0}".format(execution_date.isoformat())

    dr = DagRun.find(dag_id=dag_id, run_id=run_id)
    if dr:
        flash("This run_id {} already exists".format(run_id))
        return redirect(origin)

    run_conf = {}
    conf = request.values.get("conf")
    if conf:
        try:
            run_conf = json.loads(conf)
        except ValueError:
            flash("Invalid JSON configuration", "error")
            return self.render(
                "airflow/trigger.html",
                dag_id=dag_id,
                origin=origin,
                conf=conf,
            )

    dag.create_dagrun(
        run_id=run_id,
        execution_date=execution_date,
        state=State.RUNNING,
        conf=run_conf,
        external_trigger=True,
    )

    flash("Triggered {}, it should start any moment now.".format(dag_id))
    return redirect(origin)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.7/site-packages/airflow/www_rbac/decorators.py", line 121, in wrapper
return f(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_appbuilder/security/decorators.py", line 109, in wraps
return f(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/www_rbac/decorators.py", line 56, in wrapper
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/www_rbac/views.py", line 1050, in trigger
external_trigger=True
File "/usr/local/lib/python3.7/site-packages/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/models/dag.py", line 1818, in create_dagrun
return self.get_dag().create_dagrun(run_id=run_id,
AttributeError: 'NoneType' object has no attribute 'create_dagrun'

# Fixed code
def trigger(self, session=None):
    dag_id = request.values.get("dag_id")
    origin = request.values.get("origin") or "/admin/"

    if request.method == "GET":
        return self.render(
            "airflow/trigger.html", dag_id=dag_id, origin=origin, conf=""
        )

    dag = (
        session.query(models.DagModel).filter(models.DagModel.dag_id == dag_id).first()
    )
    if not dag:
        flash("Cannot find dag {}".format(dag_id))
        return redirect(origin)

    execution_date = timezone.utcnow()
    run_id = "manual__{0}".format(execution_date.isoformat())

    dr = DagRun.find(dag_id=dag_id, run_id=run_id)
    if dr:
        flash("This run_id {} already exists".format(run_id))
        return redirect(origin)

    run_conf = {}
    conf = request.values.get("conf")
    if conf:
        try:
            run_conf = json.loads(conf)
        except ValueError:
            flash("Invalid JSON configuration", "error")
            return self.render(
                "airflow/trigger.html",
                dag_id=dag_id,
                origin=origin,
                conf=conf,
            )

    dag = dagbag.get_dag(dag_id)
    dag.create_dagrun(
        run_id=run_id,
        execution_date=execution_date,
        state=State.RUNNING,
        conf=run_conf,
        external_trigger=True,
    )

    flash("Triggered {}, it should start any moment now.".format(dag_id))
    return redirect(origin)