Issue
I have been working on my company website in flask and I am trying to make the contact page work with URL_FOR, however it does not seem to be accepting it. I am not sure where I have gone wrong with the code. I am sure I am missing something or did something wrong. I have been working at this most of the morning. If anyone is able to help out that would be awesome..
Thank you in advance. Michael
from flask import Flask, render_template, request, redirect, url_for
import csv
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<string:page_name>')
def html_page(page_name):
return render_template(page_name)
# def write_to_file(data):
# with open('database.txt', mode='a') as database:
# name = data["name"]
# email = data["email"]
# message = data["message"]
# file = database.write(f'\n{name},{email},{message}')
def write_to_csv(data):
with open('database.csv', newline="", mode='a') as database2:
name = data["name"]
email = data["email"]
message = data["message"]
csv_writer = csv.writer(database2, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow([name, email, message])
@app.route('/contact', methods=['POST', 'GET'])
def contact():
if request.method == 'POST':
data = request.form.to_dict()
write_to_csv(data)
return redirect('/sent.html')
else:
return 'Something went wrong. Try again..!'
html contact page is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guru Coding | Contact Us</title>
<meta name="description"
content="Guru Coding services. We design and develop high quality websites tailored to your needs">
<link rel="icon" type="image/png" href="static/assets//favicon.png" />
<link rel="stylesheet" href="static/style/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css"
integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w=="
crossorigin="anonymous" />
</head>
<body>
<!-- Navigation container -->
<nav id='navMenu'>
<!-- Top Nav wrapper -->
<div class="top__nav__wrapper">
<!-- Logo container -->
<a href="index.html">
<div class="logo__container" title='Guru Coding logo'>
<!-- Logo -->
<img class='logo' src="static/assets/favicon.png" loading='lazy' alt="Guru Coding logo">
</div>
</a>
</div>
<!-- Nav list container -->
<div class="nav__list__container">
<ul>
<li title='Home'><a href="index.html">Home</a></li>
<li title='Services'><a href="services.html">Services</a></li>
<!-- <li title='Portfolio'><a href="portfolio.html">Portfolio</a></li> -->
<li title='Our Team'><a href="team.html">Our Team</a></li>
<li title='Contact' class='page--active'><a href="contact.html">Contact</a></li>
</ul>
</div>
</nav>
<!-- Burger icon container -->
<div class="burger__container" id='navIcon'>
<!-- Burger lines -->
<div class="burger__line line1"></div>
<div class="burger__line line2"></div>
<div class="burger__line line3"></div>
</div>
<!-- Header container -->
<header>
<!-- Header text container -->
<div class="text__container">
<!-- Header title container -->
<div class="header__container header__container--contact">
<h1 class='highlight__title'>Contact Us</h1>
</div>
<!-- Header text container -->
<div class="header__text">
<!-- Header text -->
<h4>You have a business idea and you want to make it a <span class="highlight__text">real
thing?</span>
<br>
<br>
Or maybe you want a simple website for your <span class="highlight__text">personal needs?</span>
<br>
<br>
Whatever it is you
are looking for, you can surely find it at
<span class="highlight__text">Guru Coding!</span>
</h4>
</div>
</div>
<!-- Scroll icon container -->
<div class="icon__container">
<!-- Scroll icon -->
<i class="fas fa-sort-down"></i>
</div>
</header>
<!-- Section 1 container -->
<section class='section--1 section--contact'>
<h2 class='section__title'>Get in touch</h2>
<!-- Social Media icons container -->
<div class="social__icons__container">
<!-- Twitter link and icon -->
<a href="https://twitter.com/GuruCodingCo" title='Guru Coding Twitter' target="_blank">
<i class="fab fa-twitter-square"></i>
</a>
<!-- Facebook link and icon -->
<a href="https://www.facebook.com/Guru-Coding-Company-101417465307382" title='Guru Coding Facebook'
target="_blank">
<i class="fab fa-facebook-square"></i>
</a>
</div>
<!-- Contact form container -->
<div class="contact__form__container">
<form method="post" action="{{ url_for ('sent') }}" id='contactForm'>
<div class="input__container">
<label for="name">Name</label>
<input type="text" id='name' placeholder='Name'>
</div>
<div class="input__container">
<label for="email">Email</label>
<input type="text" id='email' placeholder='Email'>
</div>
<div class="input__container">
<textarea type="text" placeholder='Enter your message'></textarea>
</div>
<div class="button__container">
<button type='submit' id='submitButton' style='color: whitesmoke'>Submit</button>
</div>
</form>
</div>
</section>
<!-- Footer section -->
<footer>
<!-- Footer left -->
<div class="footer--left">
<p>Guru Coding 2021</p>
<br>
<a href="sitemap.html" class='highlight__link' title='Sitemap'>View Sitemap</a>
</div>
<!-- Footer right -->
<div class="footer--right">
<p>Website created by
<a title='Contact Author' class='highlight__link' href="mailto:[email protected]">Lukasz</a>
</p>
</div>
</footer>
<!-- Script tags -->
<script src="static/app.js"></script>
</body>
</html>
Solution
The problem here is you dont have a route for 'sent'
anywhere in your code so I think you meant 'contact'
for the form post method.
Answered By - SaGaR
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.