0

I face a function undefined error when I run the server (FLASK app):

NameError: name 'format_date' is not defined enter image description here

This is all my code so far:

from flask import Flask, render_template
import markdown
import os
from werkzeug import cached_property
import yaml

POSTS_FILE_EXTENSION = '.md'
app = Flask(__name__)

class Post(object):
    def __init__(self,path):
        self.path=path
        self._initialize_metadata()

    @cached_property
    def html(self):
        with open(self.path, 'r') as fin:
            content= fin.read().split('\n\n',1)[1].strip()
        return markdown.markdown(content)

    def _initialize_metadata(self):
        content = ''
        with open(self.path, 'r') as fin:
            for line in fin:
                if not line.strip():
                    break
                content +=line
        self.__dict__.update(yaml.load(content))

    def format_date(value, format='%B %d, %Y'):
        return value.strftime(format)

@app.route('/')
def index():
    return 'Hello, world!'

@app.route('/blog/<path:path>')
def post(path):
    #import ipdb; ipdb.set_trace()
    path = os.path.join('posts', path+'.md')
    post = Post(path)
    return render_template('post.html', post=post, format_date=format_date)


if __name__ == '__main__':
    app.run(port=8000, debug=True)

Why does it say undefined? I defined it. :|

post.html:

<h3 id='date'>{{format_date(post.date)}}</h3>

hello.md:

title: Hello, worldtitle!
date: 2013-03-25

# Hello WOrld

### This is an H3 level header

P.S. : Don't judge me, I'm a noob.

6
  • What should I import? Commented Mar 2, 2016 at 16:36
  • You're missing the actual stacktrace, which probably happens to be on the render_template line, because you have the function defined in another file, not the one that contains your post function Commented Mar 2, 2016 at 16:37
  • Can you show the file structure, and say which file you defined the function in and which one your route is defined in? You need to import the function from the module. Commented Mar 2, 2016 at 16:37
  • import the function when rendering the Jinja2 template.... jinja.pocoo.org/docs/dev/api Commented Mar 2, 2016 at 16:39
  • Well, format_date does not exist. Post.format_date exists - as a method defined in class. In Python indentation is crucial - it's part of a syntax. Please fix it. Commented Mar 2, 2016 at 16:47

1 Answer 1

2

You are trying to use the format_date function that is defined on the class. You could either define it as a stand alone function, or simply use the method on the Post class. You would need to modify it a bit

class Post(object):
    def __init__(self,path):
        ...

    def format_date(self, format='%B %d, %Y'):
        return self.date.strftime(format)

And in rendering the template:

 return render_template('post.html', post=post)

Then you wouldn't need to pass in the function name, and instead just call

<h3 id='date'>{{post.format_date()}}</h3>

However, this is assuming you have the date property defined on your class somewhere (which in your existing template you are assuming you do, but I don't see it anywhere).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.