0

I just installed flask-bootstrap and run it in my localhost. But I always getting Internal Server Error message when visiting "/dinner/" route. How to fix this error? Thanks

Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Main.py

from flask import Flask, render_template 
from flask_bootstrap import Bootstrap

app = Flask(__name__)
bootstrap = Bootstrap(app)


@app.route('/dinner/')
@app.route('/dinner/<food>')
def index(food):
    return render_template('food.html', food=food, list=['sushi','pizza','hamburger'])

@app.errorhandler(404)
def page_not_found(error):
    return render_template('page_not_found.html'), 404

food.html

{% extends "bootstrap/base.html" %}

{% block title %} What is for dinner? {% endblock %}

{% block content %}

<div class="container">
    {% if food %}
        <div class="alert alert-success">
        <h1>I want that {{food}}</h1>
    {% else %}
        <div class="alert alert-info">
        <h1>Anything is fine!</h1>
    {% endif %}
        </div>

    {% if list %}
    <ul>
        {% for n in list %}
        <li><a href="/dinner/{{n}}">{{n}}</a></li>
        {% endfor %}
    </ul>
    {% endif %} 
</div>

{% endblock %}
flyingduck92
  • 1,449
  • 2
  • 23
  • 39
  • @app.route('/dinner/') @app.route('/dinner/') This shows that if you enter any of the two URL, the index function will be called. In one of the URL you are passing some argument and index function will take that argument, but when you are passing 1st URL, no argument is passed which is conflicting with index function as it needs some argument. – Shivendra Pratap Kushwaha Jan 22 '20 at 06:03
  • Try with the second URL and hopefully it should work if you pass some argument along with the URL. – Shivendra Pratap Kushwaha Jan 22 '20 at 06:05
  • @ShivendraPratapKushwaha the second URL is working, but the first one didn't work. Should I give another render template before @app.route('/dinner/') ? – flyingduck92 Jan 22 '20 at 07:25
  • @ShivendraPratapKushwaha how to tackle the 1st URL error and i don't got the error? – flyingduck92 Jan 22 '20 at 08:15
  • You got it right. As per my knowledge about your application. Flow is to get a page having list all the food stuff with the href links. And if you click the link you will be redirected to another page. So to get this, function written in '/dinner' should contain a render template of page having food list and then you can click on the link in that page to get redirected to /dinner/. – Shivendra Pratap Kushwaha Jan 22 '20 at 09:14

2 Answers2

0

I fixed the code with this code and it works for me:

@app.route('/dinner/')
@app.route('/dinner/<food>')
def dinner(food=None):
    return render_template('food.html', food=food, list=['sushi','pizza','hamburger'])
flyingduck92
  • 1,449
  • 2
  • 23
  • 39
-1
@app.route('/dinner/') 
@app.route('/dinner/<food>') 

This shows that if you enter any of the two URL, the index function will be called. In one of the URL you are passing some argument and index function will take that argument, but when you are passing 1st URL, no argument is passed which is conflicting with index function as it needs some argument

Better way of doing that is to do this :

@app.route('/dinner/', defaults={'food':None})
@app.route('/dinner/<food>')

This will handle the default case if you don't give any parameter.