Filling up a field in a database from a text file, flask

29 views
Skip to first unread message

Nancy Andeyo

unread,
Aug 19, 2022, 4:13:50 PM8/19/22
to sqlalchemy
I am trying to fill up a field in a table database with contents of a text file, but get None as the response when I run the code. Any assistance will be appreciated. Here is my code:

# view function - routes.py
.......
@app.route('/add_vlans', methods = ['GET', 'POST'])
@login_required
def add_vlans():
    form = AddVlanForm(current_user.routername)
    if form.validate_on_submit():
        with open("show_vlans", "r") as vlans:
            vlan_output = vlans.read()
            rt = Router(raw_vlans=vlan_output) #raw_vlans - field variable name
            db.session.add(rt)
            db.session.commit()
    return render_template('_show_vlans.html', title='Router Vlans')

#forms.py
class AddVlanForm(FlaskForm):
    raw_vlans = TextAreaField('Router vlan output:', validators=[Length(min=0, max=140)])
    submit = SubmitField('Get Vlans on Router')

#templates   - router.html
{% extends "base.html" %}

{% block content %}

    <h1>Router: {{ router.routername }}</h1>
   
    {% if router.about_router %} <p>About router: {{ router.about_router }}</p> {% endif %}
   
    Vlans on {{ router.routername }}<br>
    {% for vlan in vlans %}
        <ul>
            <li>{% include '_vlan.html' %}</li>
        </ul>
    {% endfor %}
    {% if router == current_user %}
        <p> <a href="{{ url_for('edit_router') }}"> Edit Router </a> </p>
    {% endif %}
    <h1> Vlan Configurations </h1>
        <a href="{{ url_for('add_vlans') }}"> Show Router Vlans </a>

    <p> {% include '_show_vlans.html' %} </p>
       
{% endblock %}

#sub-template -  _show_vlans.html
<table>
    <tr>
        <td>Vlans on router {{ current_user.routername }}:
            <pre>{{ current_user.raw_vlans }}</pre>
        </td>
    </tr>
</table>

I get the response:
Vlans on router router20:            None

Jonathan Vanasco

unread,
Aug 22, 2022, 11:00:48 AM8/22/22
to sqlalchemy
You should ask this in a Flask discussion group or stackoverflow.  This is a sqlalchemy group and most users here have no experience with Flask.

Siarhei Siarhei

unread,
Aug 23, 2022, 12:08:40 PM8/23/22
to sqlalchemy
Reply all
Reply to author
Forward
0 new messages