title: NameError: global name 'User' is not defined
body: <p>I follow <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vi-profile-page-and-avatars" rel="nofollow">miguelgrinberg </a> tutorial and want to make a profile page without <strong>Login page</strong> so when I have http:.../user/peg As I said I got error,I think because it couldn't connect with database. I put data in User with python command</p>
  
  <pre><code>&gt;&gt;&gt; u = models.User(nickname='peg', email='peg@email.com')
  &gt;&gt;&gt; db.session.add(u)
  &gt;&gt;&gt; db.session.commit()
  </code></pre>
  
  <p>I have a question that where is the database that store User and Post? And why he didn't define the table?( because in app.db we have a table with three column 1.reponsity_id 2. reponsity_path 3. id) and please tell me how can I solve the error. Thanx</p>
  
  <p>views.py</p>
  
  <pre><code>from app import app
  from flask import render_template, flash, redirect
  
  
  @app.route('/')
  @app.route('/index')
  def index():
      user = {'nickname': 'Miguel'}
      posts = [
         {
           'author': {'nickname': 'John'},
           'body': 'Beautiful day in Portland!'
           },
         {
           'author': {'nickname': 'Susan'},
           'body': 'The Avengers movie was so cool!'
         }
      ]
      return render_template("index.html",
                             title='Home',
                             user=user,
                             posts=posts)
  
   @app.route('/user/&lt;nickname&gt;')
   def user(nickname):
       #here I got error NameError: global name 'User' is not defined
       user = User.query.filter_by( nickname = nickname).first() 
       posts = [
            {'author': user, 'body': 'Test post #1'},
            {'author': user, 'body': 'Test post #2'}
       ]
       return render_template('user.html',
                 user=user,
                 posts=posts)
  </code></pre>
  
  <p>Models.py</p>
  
  <pre><code>from app import db
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      nickname = db.Column(db.String(64), index=True, unique=True)
      email = db.Column(db.String(120), index=True, unique=True)
      posts = db.relationship('Post', backref='author', lazy='dynamic')
  
      def __repr__(self):
          return '&lt;User %r&gt;' % (self.nickname)
  
  class Post(db.Model):
      id = db.Column(db.Integer, primary_key = True)
      body = db.Column(db.String(140))
      timestamp = db.Column(db.DateTime)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  
      def __repr__(self):
         return '&lt;Post %r&gt;' % (self.body)
  </code></pre>
  
  <p>db_create.py</p>
  
  <pre><code>#!flask/bin/python
  from migrate.versioning import api
  from config import SQLALCHEMY_DATABASE_URI
  from config import SQLALCHEMY_MIGRATE_REPO
  from app import db
  import os.path
  db.create_all()
  
  if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
      api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
      api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
  else:
      api.version_control(SQLALCHEMY_DATABASE_URI,
      SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
  </code></pre>
  
  <blockquote>
    <p>user.html</p>
  </blockquote>
  
  <pre><code>&lt;!-- extend base layout --&gt;
  {% extends "base.html" %}
  
  {% block content %}
  &lt;h1&gt;User: {{ user.nickname }}!&lt;/h1&gt;
  &lt;hr&gt;
  {% for post in posts %}
  &lt;p&gt;
  {{ post.author.nickname }} says: &lt;b&gt;{{ post.body }}&lt;/b&gt;
   &lt;/p&gt;
  {% endfor %}
  {% endblock %}
  </code></pre>