*我正在尝试制作flask,动态生成指向每个“1.index.html”的链接(*存在于中的是“项目文件夹”,它本身位于“项目”文件夹)
*链接必须是项目文件夹的名称,并指向其中的“1.index.html”
*我已经做了所有事情,一切正常,唯一的问题是flask不允许链接到templates文件夹中不存在的html文件,因此当我单击链接时,会出现错误
jinja2.exceptions.TemplateNotFound: projects\0 . market\1 . index.html
这是文件夹的结构&;文件:
/flask-app
app.py
/templates
0 . base.html
1 . index.html
2 . home.html
3 . about.html
4 . contact.html
6 . projects.html
/Projects
/0 . market
1 . index.html
..other files...
/1 . chat
1 . index.html
..other files...
/2 . stocks
1 . index.html
..other files...
..other files...
这是app.py文件
from flask import Flask, request, render_template
import os
class fodler_prop:
def __init__(self, file_name):
self.real_name = file_name #exemple ("0 . Flask")
self.folder_number = file_name[0] #exemple ("0")
self.url = file_name[4 :] #exemple ("Flask")
class file_prop:
def __init__(self, file_name):
self.real_name = file_name #exemple ("0 . index.html")
self.folder_number = file_name[0] #exemple ("0")
self.url = file_name[4 : -5] #exemple ("index")
class project_prop:
def __init__(self, real_name, url, folder_number, x, v):
self.real_name = real_name
self.url = url
self.folder_number = folder_number
self.position = x # position in table
self.validation = v # True if there is an 'index.html" file for that project
app = Flask(__name__)
@app.route("/")
def index():
return render_template("1 . index.html")
@app.route("/home")
def home():
return render_template("2 . home.html")
@app.route("/about")
def about():
return render_template("3 . about.html")
@app.route("/contact")
def contact():
return render_template("4 . contact.html")
@app.route("/projects")
def projects():
projects_folder_path = "E:\\Mbarki\\My Projects\\Web development\\0 . Flask-Projects-Manager\\templates\\projects"
projects_names = os.listdir(projects_folder_path)
projects_names_list = [] # all projects folders names are here
for project_name in projects_names:
project_name = fodler_prop(project_name)
projects_names_list.append(project_name)
valid_projects_list = [] # all valid projects names are here (projects that contains and index.html file)
all_projects_final_form = [] # all projects in their final form
c = -1 # this will count the number of projects folders
x = -1 # this save the position of the valid projects in the table "valid_projects_list"
for project_name in projects_names_list:
c += 1
x += 1
project_path = f"E:\\Mbarki\\My Projects\\Web development\\0 . Flask-Projects-Manager\\templates\\projects\\{project_name.real_name}"
project_files_names = os.listdir(project_path)
for project_file_name in project_files_names:
project_file_name = file_prop(project_file_name)
if f"{project_file_name.url}.html" == "index.html":
project_file_name = project_prop(project_name.real_name, project_name.url, project_name.folder_number, x, True)
valid_projects_list.append(project_file_name)
project_file_name = project_prop(project_name.real_name, project_name.url, project_name.folder_number, c, True)
all_projects_final_form.append(project_file_name)
break
else:
project_file_name = project_prop(project_name.real_name, project_name.url, project_name.folder_number, c, False)
all_projects_final_form.append(project_file_name)
break
return render_template("5 . projects.html", all_projects_final_form = all_projects_final_form)
@app.route("/projects/<link>") # THE PROBLEM IS HERE
def routes(link): # THE PROBLEM IS HERE
return render_template(f'projects\\{link}\\1 . index.html ') # THE PROBLEM IS HERE
if __name__ == "__main__":
app.run(debug=True)
这是“6.projects.html”文件:
[{% extends '0 . base.html' %}
{% block title %}
Projects
{% endblock %}
{% block content %}
<div class="content">
<h1 class="tha">Projects Table</h1>
<table class="table1">
<thead>
<tr>
<!-- Your Columns HERE -->
<th scope="col">Projects Names</th>
<th scope="col">Discription</th>
</tr>
</thead>
<tbody>
<!-- Your rows inside the table HERE: -->
{% for project in all_projects_final_form %}
{% if project.validation == True %}
<tr>
<td><a href="/projects/{{project.real_name}}">{{ project.real_name }}</a></td>
<td>None</td>
</tr>
{% else %}
<tr></tr>
<td>
<p class="not_active">{{ project.real_name }}</p>
</td>
<td>
<p class="not_active">(- we couldn't find the "index.html" file for this project -)</p>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
以下是“6.projects.html”文件的图像:
https://drive.google.com/file/d/1oXQtvXPJTRt8ebqmP6Ktvq4_tiK4Y_Uh/view?usp=sharing
当我点击其中一个链接时,这里是我得到的错误图像:
jinja2.exceptions.TemplateNotFound: projects\0 . market\1 . index.html
https://drive.google.com/file/d/18NcEJPF-Dy9i4TFHs44OQVne3JyUucdO/view?usp=sharing
提醒:此代码中的所有内容都正常,并且运行良好,唯一的问题是我无法链接不在templates目录中的html文件
我在自己的子目录项目中也做了类似的事情,我的工作类似于以下内容:
如果有帮助,请告诉我