Flask为每个用户创建一个唯一的个人资料页面

时间:2013-11-28 08:41:25

标签: python flask

我想使用Flask为我网站上的每个注册用户创建一个独特的个人资料页面。目前,我的个人资料页面的代码是:

@app.route('/profile/')
def profile():

if 'email' not in session:
    return redirect(url_for('signin'))

user = User.query.filter_by(email = session['email']).first()

if user is None:
    return redirect(url_for('signin'))
else:
    return render_template('profile.html')

但是,当我尝试更改

@app.route('/profile/<lastname>')
def profile(lastname):

我得到了一个werkzeug.routing.BuildError。我的问题是,我是否正在为每个用户创建一个独特的个人资料页面,如果是这样,我如何将变量添加到我的网址,以便我不会得到BuildError?谢谢您的帮助。

以下是完整的routes.py代码:

from intro_to_flask import app
from flask import Flask, render_template, request, flash, session, redirect, url_for
from forms import ContactForm, SignupForm, SigninForm
from flask.ext.mail import Message, Mail
from models import db, User

mail = Mail()

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    form = ContactForm()

    if request.method == 'POST':
        if form.validate_on_submit() == False:
            return render_template('contact.html', form=form)
        else:        
            name = request.form['name']
            subject = request.form['subject']
            senderEmail = request.form['email']
            message = request.form['message']
            msg = Message(subject, sender = senderEmail, recipients=    ['myemailaddress@gmail.com'])
        msg.body = message + "\n\n" + name + " (" + senderEmail + ")"
        mail.send(msg)
        return render_template('contact.html', success=True)

elif request.method == 'GET':
    return render_template('contact.html', form=form)

@app.route('/signup', methods = ['GET', 'POST'])
def signup():
    form = SignupForm()    

    if 'email' in session:
        return redirect(url_for('profile'))

    if request.method == 'POST':
        if form.validate() == False:
            return render_template('signup.html', form = form)
        else:
            newuser = User(form.firstname.data, form.lastname.data, form.email.data,  form.password.data)
            db.session.add(newuser)
            db.session.commit()

            session['email'] = newuser.email

            return redirect(url_for('profile'))

    elif request.method == 'GET':
        return render_template('signup.html', form = form)

@app.route('/profile/')
def profile():

    if 'email' not in session:
        return redirect(url_for('signin'))

    user = User.query.filter_by(email = session['email']).first()

    if user is None:
        return redirect(url_for('signin'))
    else:
        return render_template('profile.html')

@app.route('/signin', methods=['GET', 'POST'])
def signin():
    form = SigninForm()    

    if 'email' in session:
        return redirect(url_for('profile'))

    if request.method == 'POST':
        if form.validate() == False:
            return render_template('signin.html', form=form)
        else:
            session['email'] = form.email.data
            return redirect(url_for('profile'))

    elif request.method == 'GET':
        return render_template('signin.html', form = form) 

@app.route('/signout')
def signout():

    if 'email' not in session:
        return redirect(url_for('signin'))

    session.pop('email', None)
    return redirect(url_for('home'))

我认为BuildError来自于我调用url_for('profile')但我的profile函数将lastname作为参数,并且它没有反映在我的url_for命令中。我只是不确定我应该在url_for方法中包含哪个参数。

这是我的models.py:

的代码
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash

db = SQLAlchemy()

class User(db.Model):
    __tablename__ = 'users'
    uid = db.Column(db.Integer, primary_key = True)
    firstname = db.Column(db.String(100))
    lastname = db.Column(db.String(100))
    email = db.Column(db.String(120), unique = True)
    pwdhash = db.Column(db.String(54))    

    def __init__(self, firstname, lastname, email, password):
        self.firstname = firstname.title()
        self.lastname = lastname.title()
        self.email = email.lower()
        self.set_password(password)

    def set_password(self, password):
        self.pwdhash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.pwdhash, password)

2 个答案:

答案 0 :(得分:1)

这里有2个问题:

  1. 验证
  2. 网址路由
  3. 验证

    你做错了。为什么在会话中存储电子邮件地址并且每次都在函数中访问它? 我建议你使用名为Flask-Login的烧瓶扩展。 它为您处理一切,并为您的登录用户公开current_user代理。

    有关参考实施,请按照官方网站上的教程或查看this Flask mini framework

    网址路由

    你定义了:

    @app.route('/profile/<lastname>')
    def profile(lastname):
    

    所以lastname是强制性参数。你需要以这种方式调用它:

    url_for('.profile', lastname='foo')
    

答案 1 :(得分:-1)

也许本指南可以帮助您获得您想要做的事情。 Building a Flask-Blog: Part1