in Flask, PYTHON, Web Development

Change of plans…sort of…kind of…

I am currently reconsidering my options as far as the database for FlaskCasts. As a newbie to Flask and Python, I am starting to see that MongoDB may be more challenging to use than I had initially thought. However, I’m not one to give up so soon. A couple of things I am confused about stand out:

  1. Implementing Pagination for the list of videos on the front page.
  2. Sorting those in descending order.

Coming from an SQL background this isn’t so complicated and in fact a lot of the SQLAlchemy based libraries have a pagination method built in.

Right now I am looking at the Flask-PyMongo extension to see if this will make things any easier. So far I have used plain PyMongo (on my blog attempt I mentioned earlier) and wrote a lot of the DB methods myself. I’ve already came across YouTube videos showing how to sort with this library so I think I may be able to use this and roll my own pagination.

After a little more research, I found Flask-MongoEngine. With just a little tinkering I was able to implement a very basic pagination. So I am pretty happy with that. This library seems to be well supported so I may just stick with this.

[pastacode lang=”python” manual=”from%20flask%20import%20Flask%2C%20request%2C%20Blueprint%2C%20render_template%0Afrom%20flask_mongoengine%20import%20*%0Afrom%20.models%20import%20Post%0A%0Aapp%20%3D%20Flask(__name__)%0Aapp.config.from_object(‘config’)%0Adb%20%3D%20MongoEngine(app)%0A%0A%40app.route(‘%2F’)%0Adef%20hello_world()%3A%0A%20%20%20%20page%20%3D%20int(request.args.get(‘page’)%20or%201)%0A%20%20%20%20posts%20%3D%20Post.objects.order_by(‘-created’)%0A%20%20%20%20paginated_posts%20%3D%20Pagination(posts%2C%20page%3Dpage%2C%20per_page%3D2)%0A%20%20%20%20return%20render_template(‘index.html’%2C%20paginated_posts%3Dpaginated_posts)” message=”” highlight=”” provider=”manual”/]

This is my view. Basically it checks to see if a page variable is being passed through. Gets the posts in descending order. Passes the posts to the Pagination class that is built in with the current page and showing only 2 entries per page. Then it renders the template with the paginated_posts.

[pastacode lang=”python” manual=”!’hello_world’)%20%7D%7D%0A” message=”” highlight=”” provider=”manual”/]

I’d never really messed wtih a Macro in Jinja before this so I need to do a little more studying to get a grasp on what is happening here. ┬áBut…it works, and is much easier to roll out than anything else I think I could have done.

The models are a bit confusing as these classes inherit from the Document class…and I have not yet worked with anything like this, I’ve written my own classes. I am going to play around with the concepts a little more to get confortable. Here is what they look like right now:

[pastacode lang=”python” manual=”import%20datetime%0Afrom%20mongoengine%20import%20*%0A%0A%0Aclass%20User(Document)%3A%0A%20%20%20%20email%20%3D%20EmailField()%0A%20%20%20%20password%20%3D%20StringField()%0A%20%20%20%20fullname%20%3D%20StringField()%0A%0A%0Aclass%20Post(Document)%3A%0A%20%20%20%20title%20%3D%20StringField()%0A%20%20%20%20content%20%3D%20StringField()%0A%20%20%20%20author%20%3D%20ReferenceField(User)%0A%20%20%20%20created%20%3D%20DateTimeField(” message=”” highlight=”” provider=”manual”/]

Going to work on this some more tomorrow. Very happy to have solved this pagination issue however.


Write a Comment