python - Flask post url is not redirecting dynamic link -
i having problems on getting tutorial flask on displaying dynamic link on blog post. dynamic link suppose http://localhost:8000/blog/hello
but got gibberish link instead http://localhost:8000/%3cbound%20method%20post.url%20of%20%3c__main__.post%20object%20at%200x7f04ddd9dd50%3e%3e
import os import sys import collections flask import flask, render_template, url_for, abort, request flask.ext.frozen import freezer werkzeug import cached_property werkzeug.contrib.atom import atomfeed import markdown import yaml string import strip posts_file_extension = '.md' class sorteddict(collections.mutablemapping): def __init__(self, items= none, key=none, reverse=false): self._items = {} #python dict self._keys =[] #python list if key: self._key_fn = lambda k: key(self._items[k]) else: self._key_fn = lambda k: self._items[k] self._reverse = reverse if items not none: self.update(items) #abstract base class def __getitem__(self, key): return self._items[key] def __setitem__(self, key, value): self._items[key]= value if key not in self._keys: self._keys.append(key) self._keys.sort(key= self._key_fn, reverse= self._reverse) def __delitem__(self, key): self._items.pop(key) self._keys.remove(key) def __len__(self): return len(self._keys) def __iter__(self): key in self._keys: yield key def __repr__(self): return '%s(%s)' % (self.__class__.__name__, self._items) class blog(object): def __init__(self, app, root_dir='',file_ext=posts_file_extension): self.root_dir = root_dir self.file_ext = file_ext self._app =app #_ private , internal self._cache = sorteddict(key = lambda p: p.date, reverse= true) self._initialize_cache() @property def posts(self): return self._cache.values() def get_post_or_404(self, path): """ returns post object given path or raises notfound exception""" try: return self._cache[path] except keyerror: abort(404) def _initialize_cache(self): """walks root directory , adds posts cache """ (root, dirpaths, filepaths) in os.walk(self.root_dir): filepath in filepaths: filename, ext = os.path.splitext(filepath) if ext == self.file_ext: path = os.path.join(root, filepath).replace(self.root_dir,'') post = post(path, root_dir = self.root_dir) self._cache[post.urlpath] = post class post(object): def __init__(self, path, root_dir=''): self.urlpath = os.path.splitext(path.strip('/'))[0] self.filepath = os.path.join(root_dir, path.strip('/')) self._initialize_metadata() @cached_property def html(self): open(self.filepath, 'r') fin: content = fin.read().split('\n\n',1)[1].strip() return markdown.markdown(content) def url(self,_external= false): return url_for('post',path=self.urlpath, _external=_external) def _initialize_metadata(self): content = '' open(self.filepath, 'r') fin: line in fin: if not line.strip(): break content += line self.__dict__.update(yaml.load(content)) app = flask(__name__) blog = blog(app, root_dir= 'posts') freezer = freezer(app) @app.template_filter('date') def format_date(value, format = '%b %d, %y'): datetime import date, datetime, time return value.strftime(format) @app.route('/') def index(): return render_template('index.html', posts= blog.posts) @app.route('/blog/<path:path>/') def post(path): #import ipdb; ipdb.set_trace() #import pdb; pdb.set_trace() #path = os.path.join('posts', path + posts_file_extension) #post = post(path) post = blog.get_post_or_404(path) #post = post(path + posts_file_extension, root_dir = 'posts') return render_template('post.html', post=post) @app.route('/feed.atom') def feed(): feed = atomfeed('recent articles', feed_url=request.url, url=request.url_root) posts = blog.posts[:10] title = lambda p: '%s: %s' %(p.title, p.subtitle) if hasattr(p, 'subtitle') else p.title post in posts: feed.add(title(post), unicode(post.html), content_type='html', author='mancube', url=post.url(_external= true), updated=post.date, published=post.date) return feed.get_response() if __name__ == '__main__': if len(sys.argv) > 1 , sys.argv[1] =='build': freezer.freeze() else: post_files = [post.filepath post in blog.posts] app.run(port=8000, debug=true, extra_files = post_files)
my index.html :
{% extends "base.html" %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/index.css') }}"> {% endblock head %} {% block content %} <div id="header"> <h1>my awesome blog</h1> </div> <div id="posts"> {% post in posts %} <div class="post"> <span class="post-date">{{ post.date|date }}</span> <a href="{{ post.url }}" class="post-title"> {{ post.title}}{% if post.subtitle %}: {{ post.subtitle }} {% endif %} </a> </div> {% endfor %} </div> {% endblock content %}
i new flask, hope can me on this.
href="{{ post.url }}"
that's function, need call "post.url()".
Comments
Post a Comment