Issue
i need to make a flask template that counts every repeated word in a database column in this time named titlecol and show it in a div, kinda similar to this example: https://github.com/piyush335/Flask-WordCount. But it needs to load automatically when i open a template in the web browser without filling a form like that example.
Here is the code:
# app.py
**Dashboard**
@app.route('/dashboard')
@is_logged_in
def dashboard():
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles WHERE author = %s", [session['username']])
articles = cur.fetchall()
if result > 0:
return render_template('dashboard.html', articles=articles)
else:
msg = 'No articles found'
return render_template('dashboard.html', msg=msg)
cur.close()
**Article**
@app.route('/article/<string:id>/')
@is_logged_in
def article(id):
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article = cur.fetchone()
return render_template('article.html', article=article)
# dashboard.html
<table id="registr" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Title Name</th>
</tr>
</thead>
{% for article in articles %}
<tr>
<td>{{article.titlecol}}</td>
<td><a href="article/{{article.id}}" class="btn btn-icon btn-info btn-round pull-right"><i class="fas fa-eye"></i></a></td>
<td><a href="edit_article/{{article.id}}" class="btn btn-warning btn-icon btn-round pull-right"><i class="fas fa-user-edit"></i></td>
<td>
<form action="{{url_for('delete_article', id=article.id)}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="X" class="btn btn-danger btn-icon btn-round" onclick="return confirm('Are you sure?')">
</form>
</td>
</tr>
{% endfor %}
</table>
Solution
Just do it sql, group by some column and count the distinct of the required column.
Answered By - d swamy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.