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! While you're at it, check out some resources Treehouse students have shared here.
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
Josh Keenan
20,315 PointsSocial network in Flask Login Error
Method Not Allowed
The method is not allowed for the requested URL.
That is the error I get thrown in browser when trying to login, gonna drop in my forms.py and layout.html Confident the error is in one of the two
from flask_wtf import Form
from models import User
from wtforms import StringField, PasswordField
from wtforms.validators import (DataRequired, Regexp, ValidationError,
Email, Length, EqualTo)
def name_exists(form, field):
if User.select().where(User.username == field.data).exists():
raise ValidationError('User with that name already exists!')
def email_exists(form, field):
if User.select().where(User.email == field.data).exists():
raise ValidationError('User with that email already exists!')
class Register(Form):
username = StringField(
'Username',
validators=[
DataRequired(),
Regexp(
r'^[a-zA-Z0-9_]+$',
message=('Username should be one word, letters, '
'numbers and underscores only.')
),
name_exists
])
email = StringField(
'Email',
validators=[
DataRequired(),
Email(),
email_exists
])
password = PasswordField(
'Password',
validators=[
DataRequired(),
Length(min=2),
EqualTo('password2', message='Passwords must match')
])
password2 = PasswordField(
'Confirm Password',
validators=[DataRequired()]
)
class LoginForm(Form):
email = StringField('Email', validators=[
DataRequired(),
Email()
])
password = PasswordField('Password', validators=[
DataRequired()
])
<!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>
</nav>
{% block content %}{% endblock %}
</div>
</div>
<footer>
<div class="row">
<p>A Social App built with Flask<br>by <a href="http://teamtreehouse.com">Josh Keenan</a></p>
</div>
</footer>
<script src="/static/js/vendor/disTime.min.js"></script>
<script src="/static/js/main.js"></script>
</body>
</html>
3 Answers
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Josh
Can you post your views as well. method not allowed normally means your trying to post data to a view when it only accepts get request. Make sure your view accepts both GET and POST see below example.
@app.route('/login',methods=['GET','POST'])
def login():
#do something
Josh Keenan
20,315 PointsThat is fixed, the new bug is... (only added {% block 'content' %} instead of {% block content %}
jinja2.exceptions.TemplateSyntaxError: expected token 'name', got 'string'
no line given still...
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsSo are you saying its fixed now ? because you should be using {% block content %} and not {% block 'content' %}
Josh Keenan
20,315 Pointslots of bugs are in the code, copied directly from Kenneth, and using the quotes helped with one!
https://teamtreehouse.com/community/social-network-in-flask-error-login-and-register
new link for my struggles! Check that out
Josh Keenan
20,315 PointsJosh Keenan
20,315 PointsJosh Keenan
20,315 PointsJosh Keenan
20,315 PointsJosh Keenan
20,315 PointsJosh Keenan
20,315 PointsJosh Keenan
20,315 PointsJosh Keenan
20,315 Pointsjinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'string'
new problem...
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsAndreas cormack
Python Web Development Techdegree Graduate 33,011 Pointsis the method not allowed error fixed? in terms of expected token 'end of print statement', got 'string' error does it give you a line number? i cannot see anything wrong with the html you have posted.