Python Forum
How do you define and -put a value into a variable in Flask?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you define and -put a value into a variable in Flask?
#1
I am new to Flask and Python and have worked through a number of tutorials and then when starting n my own app, it crashed on the first line defining a variable.

How do I do such a simple thing?

Am I calling it by the correct name?

TIA, a4ginatl.
Reply
#2
Show us the code and any errors - don't make us guess what the problem is!
Reply
#3
(Aug-14-2021, 03:26 AM)ndc85430 Wrote: Show us the code and any errors - don't make us guess what the problem is!

import os
from flask import Flask, render_template, request, redirect
from datetime import datetime
from os import environ

app = Flask(__name__)

driver = "IBM i Access ODBC Driver"
system = 'a4gi501'
username = 'myID'
password = 'myPassword'
connect_ibm = 'DRIVER=' + driver + ';SYSTEM=' + system + ';UID=' + username + ';PASSWORD=' + password)

connection = pyodbc.connect('DRIVER=IBM i Access ODBC Driver;SYSTEM=a4gi501;UID=DFREINKEL;PWD=0urDogsandCats5;')

cursor = connection.cursor()


@app.route('/')
def index():
return render_template('/index.html')

if request.method == ['POST', 'GET']:
mdate_now = str(datetime.now().date)
return render_template('index.html')
else:
return render_template('index.html')


If I hardcode the values it works.
I also need it to run once at the beginning 0f the code.

TIA a4ginatl
Reply
#4
Please use Python tags when posting code, to keep the formatting and add line numbering and syntax highlighting. You can use the Python logo button to insert a pair of tags and then just post your code between them.

So what is wrong with your program? You haven't shown any errors.
Reply
#5
(Aug-14-2021, 05:33 PM)ndc85430 Wrote: Please use Python tags when posting code, to keep the formatting and add line numbering and syntax highlighting. You can use the Python logo button to insert a pair of tags and then just post your code between them.

So what is wrong with your program? You haven't shown any errors.


Thanks

Where do I find the python tags to use and how do I include line numbers? I am using Visual Code.

For starters, nothing happens. I cannot display the fields in the html. They display as blanks or empty fields.

I need to run a connection to an ODBC driver. I need to pass data into the connection string and this simply fails in flask.

I tested the link to the ODBC and that works but that may have been pure python.

I guess I don't know the relationship between flask and python and what to do when. Everything I found so far is all basic CRUD stuff which I have working but when you need to manipulate the data, I have trouble as here.

Testing the code in basic python gives me the results I am looking for but when I try to add it on the first page, it blows up or ignores the code.

None of the values in jinja display.



The following is index.html.
[html]
{% extends 'base.html' %}

{% block head %}
<title>Home</title>
{% endblock %}

{% block body %}
<br>
<h1{{ datetime }}</h1>
2021-08-12

connect_ibm = {{ connect_ibm }}
driver = {{ driver }}
system = {{ system }}
username = {{ username }}

<br>
<main role="main">
<div class="jumbotron">
<div class="col-sm-8 mx-auto">
<h1>Top 5 numbers</h1>
<br>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Position</th>
<th scope="col">Number</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>{{ rank_1 }}</td>
</tr>
<tr>
<th scope="row">2</th>
<td>{{ rank_2 }}</td>
</tr>
<tr>
<th scope="row">3</th>
<td>{{ rank_3 }}</td>
</tr>
<tr>
<th scope="row">4</th>
<td>{{ rank_4 }}</td>
</tr>
<tr>
<th scope="row">5</th>
<td>{{ rank_5 }}</td>
</tr>
<tr>
<th scope="row">6</th>
<td>{{ rank_6 }}</td>
</tr>
</tbody>
</table>

<a class="btn btn-primary" href="/posts" role="button">Refresh &raquo;</a>
</p>
</div>
</div>
</main>
{% endblock %}
[/html]
Reply
#6
No, the Python tags are just for this forum - again, use the Python logo button to put the tags in your post.

You say the code "blows up". Blows up how exactly? Again, show us error messages. We can't see your screen can we?

Also, I notice that you're using variables in your template, but in the Python code, you don't seem to be passing to render_template a dict containing those variable names and their values. How do you expect the template to be rendered with the values you want?
Reply
#7
(Aug-14-2021, 07:09 PM)ndc85430 Wrote: No, the Python tags are just for this forum - again, use the Python logo button to put the tags in your post.

You say the code "blows up". Blows up how exactly? Again, show us error messages. We can't see your screen can we?

Also, I notice that you're using variables in your template, but in the Python code, you don't seem to be passing to render_template a dict containing those variable names and their values. How do you expect the template to be rendered with the values you want?

I attached a copy of the browser.

Attached Files

Thumbnail(s)
   
Reply
#8
See my point about passing the values to your template.

You also don't need to keep quoting posts in their entirety - it just wastes space.
Reply
#9
I change my code a bit and that has more issues. I bet they are basic I don't see the issues. Its too new to me still.

I added a login page which must always be the first page. It will not display the page but displays index1.html.

import os
from flask import Flask, render_template, request, redirect
from datetime import datetime
from os import environ

app = Flask(__name__)

@app.route('/login')
@app.route('/')
def login():
    
    if request.method == ['POST', 'GET']:
        return render_template('login.html', username=username, password=password)
    else:
        return render_template('index1.html')

@app.route('/index1')
def index1():
    if request.method == ['POST', 'GET']:
        mdate_now = str(datetime.now().date)
        return render_template('index1.html')
    else:
        return render_template('index1.html')


if __name__ == "__main__":
    app.run(debug=True)

    driver = "IBM i Access ODBC Driver"
    system = 'a4gi501'
    username = 'myID'
    password = 'myPassword' 
    connect_ibm = 'DRIVER=' + driver + ';SYSTEM=' + system + ';UID=' + username + ';PASSWORD=' + password
[html]
{% extends "base.html" %} {% block title %}Login{% endblock %} {% block content
%}
<form method="POST">
<h3 align="center">Login</h3>
<div class="form-group">
<label for="email">User ID</label>
<input
type="text"
class="form-control"
id="username"
name="username"
placeholder="User Name"
/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input
type="password"
class="form-control"
id="password"
name="password"
placeholder="Enter password"
/>
</div>
<br />
<button type="submit" class="btn btn-primary">Login</button>
</form>
{% endblock %}
[/html]
Reply
#10
Lines 12 and 19: why are you checking request.method == ["POST", "GET"]? That will never be true since a request will only contain a single method. That is, the request method will be either POST or GET, not both.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020