Forum >> Programmazione Python >> Web e Reti >> SITO WEB- PROBLEMA

Pagina: 1

Salve, io e una mia amica stiamo scrivendo un programma, ma stiamo riscontrando un problema nel codice. Il sito è un blog dove l'utente per vedere i post, scriverli e commentarli deve registrarsi. Il problema è che quando l'utente si logga può scrivere post , ma è l'unico a vederli e quando commenta li salva nel database ma purtroppo non vengono visualizzati sullo schermo. Scrivo qua sotto il pezzo di codice interessato.
Grazie del tempo dedicatomi.

DA VIEWS:


@app.route('/user/<username>/post/<post_id>/', methods=['GET', 'POST'])
def post(username, post_id):
    user = User.query.filter_by(username=username).first()
    post = Post.query.filter_by(id=post_id).first()
    form = CommentForm()
    
    if user is None:
        flash('user: {} not found'.format(username))
        return redirect(url_for('home'))
        
    if post is None:
        flash('Post No.: {} not found'.format(post_id))
        return redirect(url_for('user', username=username))

    if request.method == 'GET':
        if not g.is_logged_in:
            comments = Comment.query.filter_by(post_id=post.id).order_by(Comment.created_on.desc()).all()
            flash('please login to leave a comment')
            return render_template('post.html', visitor=g.user, user=user, post=post, comments=comments, form=form)
        else:
            comments = Comment.query.filter_by(post_id=post.id).order_by(Comment.created_on.desc()).all()
            return render_template('post.html', visitor=g.user, user=user, post=post, comments=comments, form=form)
            
    if request.method == 'POST':
        if not form.validate():
            return redirect(url_for('post', username=username, post_id=post_id))
        else:
            new_comment = Comment(body=form.body.data, author=g.user)
            print 'dicks ' + form.post_id.data
            db.session.add(new_comment)
            db.session.commit()
            return redirect(url_for('post', username=username, post_id=post.id))

DA MODELS:

class User(db.Model):
    __tablename__ = "users_user"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(USER.MAX_USERNAME), unique=True)
    email = db.Column(db.String(USER.MAX_EMAIL), unique=True)
    pwdhash = db.Column(db.String(USER.MAX_PASSW))
    uuid = db.Column(db.String(66))
    created_on = db.Column(db.DateTime, default = db.func.now())
    verified = db.Column(db.Boolean(), default = False)
    
    status = db.Column(db.Integer, default=USER.ALIVE)
    role = db.Column(db.Integer, default=USER.USER)
    
    posts = db.relationship('Post', backref='author', lazy='dynamic')
    comments = db.relationship('Comment', backref='author', lazy='dynamic')
    followed = db.relationship(
        'User',
        secondary = followers,
        primaryjoin = (followers.c.follower_id == id),
        secondaryjoin = (followers.c.followed_id == id),
        backref = db.backref('followers', lazy='dynamic'),
        lazy='dynamic'
    )
    
    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.set_pwdhash(password)
        self.uuid = str(uuid.uuid1())

    def __repr__(self):
        return '<User: {}>'.format(self.username)
    
    def set_pwdhash(self, password):
        self.pwdhash = generate_password_hash(password)
    
    def check_pwdhash(self,password):
        return check_password_hash(self.pwdhash, password)

    def follow(self, user):
        if not self.is_followed(user):
            self.followed.append(user)
            return self
    
    def unfollow(self, user):
        if self.is_followed(user):
            self.followed.remove(user)
            return self
    
    def is_followed(self, user):
        return self.followed.filter(followers.c.followed_id == user.id).count() > 0
    
    def following(self):
        return self.followed.filter(followers.c.follower_id == self.id).all()
        
    def followed_posts(self):
        return Post.query.join(followers, (followers.c.followed_id == Post.user_id)).filter(followers.c.follower_id == self.id).order_by(Post.created_on.desc())


class Post(db.Model):

    __tablename__ = "users_post"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users_user.id'))
    
    title = db.Column(db.String(140), default=None)
    body = db.Column(db.String(POST.MAX_BODY), default=None)
    created_on = db.Column(db.DateTime, default=db.func.now())
    
    comments = db.relationship('Comment', backref='parent', lazy='dynamic')
    
    def __init__(self, title, body, author):
        self.title = title
        self.body = body
        self.author = author
        
    def __repr__(self):
        return '<Post: {}>'.format(self.title[:25])


class Comment(db.Model):
    
    __tablename__ = "posts_comment"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users_user.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('users_post.id'))
    
    body = db.Column(db.String(POST.MAX_BODY), default=None)
    created_on = db.Column(db.DateTime, default=db.func.now())
    
    def __init__(self, body, author):
        self.body = body
        self.author = author
    
    def __repr__(self):
        return '<Comment: {}>'.format(self.body[:25]
DA FORMS:
class PostForm(Form):
    title = StringField(
        "New Post Title",
        [
            DataRequired("Please title your post"),
            Length(
                max=140,
                message="This title exceeds the 140 character limit"
            )
        ]
    )
    body = TextAreaField(
        "New Post",
        [
            DataRequired("Please enter a new post"),
            Length(
                max=POST.MAX_BODY,
                message="This post exceeds the 10,000 character limit"
            )
        ]
    )
    submit = SubmitField("Post")

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)

    def validate(self):
        if not Form.validate(self):
            return False


class CommentForm(Form):
    body = TextAreaField(
        "New Comment",
        [
            DataRequired("Please enter a new comment"),
            Length(
                max=1000,
                message="this comment exceeds the 1000 character limit")
        ]
    )
    post_id = StringField(widget=HiddenInput())
    submit = SubmitField("Comment")

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)

    def validate(self):
        if not Form.validate(self):
            return False


--- Ultima modifica di ㎝ in data 2017-01-03 22:21:37 ---


Pagina: 1



Esegui il login per scrivere una risposta.