简体   繁体   中英

Using Gravatar in Flask Application

I want to use Gravatar to display users' profile pictures in my flask application. I followed the instructions in the documentation https://flask-gravatar.readthedocs.io/en/latest/

However, when running my application I get this error message:

application_iter = app(environ, start_response)
File "/home/ubuntu/finalproject/application.py", line 21, in <module>
gravatar = Gravatar(app,
NameError: name 'Gravatar' is not defined 

This is the beginning of the main file:

import os
import time
import datetime

from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash


# Configure application
app = Flask(__name__)

#initialize gravatar
gravatar = Gravatar(app,
                    size=80,
                    rating='g',
                    default='retro',
                    force_default=False,
                    force_lower=False,
                    use_ssl=False,
                    base_url=None)

# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True

At the moment I just want to test how it works. So my test.html just contains: {{ 'zzz.sochi@gmail.com' | gravatar }} and is called with GET and render_template.

Can someone tell me what I have to do, that the variable "Gravatar" defined in init .py is recognized?

Folder structure: Structure in CS50 IDE

The example provided in the Gravatar documentation is bad. If you do this:

gravatar = Gravatar(app,
                    size=80,
                    rating='g',
                    default='retro',
                    force_default=False,
                    force_lower=False,
                    use_ssl=False,
                    base_url=None)

... without Gravatar being defined somewhere in your file, it will fail. In this case I think you need:

from flask_gravatar import Gravatar

gravatar = Gravatar(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM