-
Notifications
You must be signed in to change notification settings - Fork 1
Post creation
Manish Sah edited this page Oct 6, 2020
·
1 revision
lets create a route so that users can create a new post
we will add a new route in routes.py
.
from blog.forms import PostForm
.
@app.route("/post/new")
@login_required
def new_post():
form = PostForm()
return render_template('create_post.html', title_name='New Post', form=form)
we will create a page called create_post.html to display our PostForm and get user input. lets create create_post.html in our templated folder and add these codes
{% extends 'layout.html' %}
{% block content %}
<div class="content-section">
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New Post</legend>
<div class="form-group">
{{ form.title.label(class="form-control-label") }}
{% if form.title.errors %}
{{ form.title(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.title.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.title(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.content.label(class="form-control-label") }}
{% if form.content.errors %}
{{ form.content(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.content.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.content(class="form-control form-control-lg") }}
{% endif %}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
{% endblock content %}
let modify the hard coded css link for main.css with the following line
previously:
<link rel="stylesheet" href="static/main.css" type="text/css">
now:
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}" type="text/css">
here, url_for finds the path to a folder so that we don't have to change the hard coded css link everytime we make some changes.
we will also add a link for New Post in our navigation bar. Add this link just above the profile link in our navigation bar.
.
.
<div class="navbar-nav">
{% if current_user.is_authenticated %}
<a class="nav-item nav-link" href="/post/new">New Post</a>
<a class="nav-item nav-link" href="/profile">Profile</a>
<a class="nav-item nav-link" href="/logout">logout</a>
{% else %}
<a class="nav-item nav-link" href="/login">Login</a>
<a class="nav-item nav-link" href="/registration">Register</a>
{% endif %}
</div>
.
let update our new_post route so that new post gets created when user submit the form
blog/routes.py
.
from blog.models import Post
@app.route("/post/new", methods=["GET", "POST"])
@login_required
def new_post():
form = PostForm()
if form.validate_on_submit():
post = Post(title=form.title.data, content=form.content.data, user_id=current_user.id)
db.session.add(post)
db.session.commit()
flash('You post has been created', 'success')
return redirect("/")
return render_template('create_post.html', title_name='New Post', form=form)
Implementation can be found over this commit