'Unable to load templates using url_for
I have a RESTful API that is built with flask, and on the home page / I want to build a web GUI into it. In order to do so I'm trying to build a flask app into the Index part of my API calls:
class Index(Resource):
def get(self):
from flask import Response, render_template, Flask
from lib.interface.settings import get_db_folders, get_file_type_count, get_recent_scans, make_keys
log_request(request, "/", "GET")
recent_scans = get_recent_scans(DATABASE_FILES_PATH, amount=10)
all_folders = get_db_folders(DATABASE_FILES_PATH)
file_types = get_file_type_count(DATABASE_FILES_PATH)
website = Flask(__name__)
@website.route("/everything")
def show_all():
return Response(render_template(
"everything.html"
))
@website.route("/upload")
def upload():
return Response(render_template(
"upload.html"
))
@website.route("/api")
def apidocs():
return Response(render_template(
"apidocs.html"
))
return Response(render_template(
"index.html", recent_scans=recent_scans,
all_scans_length=len(all_folders),
file_types=file_types, recent_scan_length=len(recent_scans) + 1
))
Everything works fine when I load into the main screen (/). I can see the index.html page. However when trying to redirect to a page called "everything.html" I get an error (the error depends on how I'm trying it as seen below):
If I use <li><span><a href="{{ url_for('.everything') }}">All Uploads</a></span></li> in everything.html the error I get is:
...
<li><span><a href="{{ url_for('templates', '.everything') }}">All Uploads</a></span></li>
TypeError: url_for() takes exactly 1 argument (2 given)
If I do <li><span><a href="{{ url_for('templates', '.everything') }}">All Uploads</a></span></li>
I get the following error:
...
raise BuildError(endpoint, values, method, self)
BuildError: Could not build url for endpoint 'everything'. Did you mean 'getstrings' instead?
If I do <li><span><a href="{{ url_for('templates', 'everything') }}">All Uploads</a></span></li> I get:
...
<li><span><a href="{{ url_for('templates', 'everything') }}">All Uploads</a></span></li>
TypeError: url_for() takes exactly 1 argument (2 given)
And so on and so forth. I'm pretty sure the problem is the way I'm trying to do this part (putting it into an API, etc) but I'm not sure if there is a work around that might work? As of right now my head.html file looks like this:
<!doctype html>
<head>
<meta charset="UTF-8">
<title>Sandbox - {% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='base.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<script src="{{ url_for('static', filename='base.js') }}"></script>
</head>
<header id="display-port">
<img id="header-image" src="{{ url_for('static', filename='base_image.png') }}"/>
</header>
<body>
<nav class="navigation-pane">
<h3 id="navigation-header">Navigation:</h3>
<ul>
<li><span><a href="{{ url_for('templates', '.everything') }}">All Uploads</a></span></li>
<li><span><a href="upload.html">Upload Files</a></span></li>
<li><span><a href="apidocs.html">API Documentation</a></span></li>
</ul>
</nav>
<section class="content">
<h3>Current File Type Counts:</h3>
<ul>
{% for key in file_types.keys() %}
<li>{{ key.upper() }}: {{ file_types[key] }}</li>
{% endfor %}
</ul>
{% block header %}{% endblock %}
{% block content %}{% endblock %}
</section>
</body>
My index.html file looks like this:
{% extends "head.html" %}
{% block title %}Home{%endblock%}
{% block header %}
<h1>Recent Scans:</h1>
{% endblock %}
{% block content %}
<h5>Total Database Scans: {{ all_scans_length }}</h5>
<ul>
{% for i in range(0, recent_scan_length) %}
<li><a href="">Hash: {{ recent_scans[i] }}</a></li>
{% endfor %}
</ul>
{% endblock %}
Directory structure looks like this:
.
├── __init__.py
├── main.py
├── static
│ ├── base.css
│ ├── base.js
│ ├── favicon.ico
│ └── base_image.png
├── templates
│ ├── apidocs.html
│ ├── everything.html
│ ├── head.html
│ ├── index.html
│ └── upload.html
├── wsgi.py
And my everything.html file has nothing in it. I think it is worth noting that I am able to load from the static folder in head.html without problem. So question being as stated above, what is causing the issue? How can I fix it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
