Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


please help me there i was just doing the builiding social network with flask using pycharm but i fail to register .

it show "Valueerror" (user aready registerd) also i can not log in . there is my code

from flask_bcrypt import check_password_hash from flask import (Flask, g, flash, render_template, redirect, url_for) from flask_login import (LoginManager, login_user, logout_user, login_required, current_user)

import forms import models

DEBUG = True PORT = 8000 HOST = ''

app = Flask(name) app.secret_key = 'auoesh,jahskd,alkjds,k_swekdius'

login_manager =LoginManager() login_manager.init_app(app) login_manager.login_view = 'login'

@login_manager.user_loader def load_user(userid): try: return models.User.get(models.User.id == userid) except models.DoesNotExist: return None

@app.before_request def before_request(): """connect to the data base before each request""" g.db = models.DATABASE g.db.connect() g.user = current_user

@app.after_request def after_request(response): """close to the data base each after request""" g.db.close() return response

@app.route('/register', methods = ('GET', 'POST')) def register(): form = forms.RegisterForm() if form .validate_on_submit(): flash("Wow ,you registered" , "success") models.User.create_user( username = form.username.data, email = form.email.data, password = form.password.data ) return redirect(url_for('index')) return render_template('register.html', form = form)

@app.route('/login', methods = ('GET', 'POST')) def login(): form = forms.LoginForm() if form.validate_on_submit(): try: user = models.User.get(models.User.email == form.email.data) except models.DoesNotExist: flash("your email or password doesn't match", "error") else: if check_password_hash(user.password, form.password.data): login_user(user) flash("you have been logged in", "success") return redirect(url_for('index')) else: flash("your email or password doesn't match", "error") return render_template('login.html',form=form)

@app.route('/logout') @login_required def logout(): logout_user() flash("you have been logged out come back soon", "success") return redirect(url_for('index'))

@app.route('/new_post', methods = ('GET', 'POST')) @login_required def post(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user._get_current_object(), content = form.content.data.strip()) flash("Message posted!" ,"Thanks!", "success!") return redirect(url_for('index')) return render_template('post.html', form=form)

@app.route('/') def index(): return 'Hey'

if name == 'main': models.initialize()

        username = 'kennethlove',
        email = "luckiusevodius@gmail.com",
        password = 'password',
        admin = True
except ValueError:

app.run(debug = DEBUG, host = HOST, port = PORT)

and this is my layout.html file contain

<!DOCTYPE html> <html class="no-js"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>{% block title %}TwoCans{% endblock %}</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="/static/css/normalize.min.css"> <link rel="stylesheet" href="/static/css/main.css"> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <script>window.html5 || document.write('<script src="/static/js/vendor/html5shiv.js"><\/script>')</script> <![endif]--> </head> <body> <header> <div class="row"> <div class="grid-33"> <a href="{{ url_for('index') }}" class="icon-logo"></a> </div> <div class="grid-33"> <!-- Say Hi --> <h1>Hello{% if current_user.is_authenticated %} {{ current_user.username }}{% endif %}!</h1> </div> <div class="grid-33"> <!-- Log in/Log out --> {% if current_user.is_authenticated %} <a href="{{ url_for('logout') }}" class="icon-power" title="Log out"></a> {% else %} <a href="{{ url_for('login') }}" class="icon-power" title="Log in"></a> <a href="{{ url_for('register') }}" class="icon-profile" title="Register"></a> {% endif %} </div> </div> </header> <!-- Flash messages --> {% with messages = get_flashed_messages(with_categories=True) %} {% if messages %} {% for category, message in messages %} <div class="notification {{ category }}">{{ message }}</div> {% endfor %} {% endif %} {% endwith %} <div class="row"> <div class="main"> <nav> <a href="{{ url_for('index') }}">All</a> {% if current_user.is_authenticated %} <a href="{{url_for('post')}}" class="new"> create new post</a> {% endif %} </nav> {% block content %}{% endblock %} </div> </div> <footer> <div class="row"> <p>A Social App built with Flask<br>by <a href="http://luckiusevodius@gmail.com">Kajoka</a></p> </div> </footer> <script src="/static/js/vendor/disTime.min.js"></script> <script src="/static/js/main.js"></script> </body> </html>

and this is my login.html file(login template)

{% extends "layout.html" %} {% from 'macros.html' import render_field %}

{% block content %} <form method = "POST" action = "" class = "form"> {{ form.hidden_tag() }} {% for field in form %} {{ render_field(field) }} {% endfor %} <button type = "submit" id = "submit">login!</button> </form> {% endblock %}