我可以制作一个 link 到一个 html 文件,它不是 flask 中的模板文件夹吗
can i make a link to an html file that is not the template folder in flask
*我正在尝试制作烧瓶,为每个“1。index.html”动态生成 links ( *存在于 它是“项目文件夹”,它本身位于“项目”文件夹中 )
*并且 link 必须是项目文件夹的名称并导致其中的“1 . index.html”
*我已经做了一切,一切正常,唯一的问题是 flask 不允许 link 到 templates 文件夹中不存在的 html 文件,所以当我点击 link 时出现错误
jinja2.exceptions.TemplateNotFound: projects[=15=] . market . 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
当我点击其中一个 link 时,这是我得到的错误图片:
jinja2.exceptions.TemplateNotFound: projects[=15=] . market . index.html
https://drive.google.com/file/d/18NcEJPF-Dy9i4TFHs44OQVne3JyUucdO/view?usp=sharing
提醒:这段代码一切正常,运行良好,唯一的问题是我不能link 不在模板目录中的 html 文件
我在自己的项目中使用子目录做了类似的事情,对我有用的类似于以下内容:
return render_template('projects/' + link + '/1 . index.html')
如果有帮助请告诉我!
*我正在尝试制作烧瓶,为每个“1。index.html”动态生成 links ( *存在于 它是“项目文件夹”,它本身位于“项目”文件夹中 )
*并且 link 必须是项目文件夹的名称并导致其中的“1 . index.html”
*我已经做了一切,一切正常,唯一的问题是 flask 不允许 link 到 templates 文件夹中不存在的 html 文件,所以当我点击 link 时出现错误
jinja2.exceptions.TemplateNotFound: projects[=15=] . market . 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
当我点击其中一个 link 时,这是我得到的错误图片:
jinja2.exceptions.TemplateNotFound: projects[=15=] . market . index.html
https://drive.google.com/file/d/18NcEJPF-Dy9i4TFHs44OQVne3JyUucdO/view?usp=sharing
提醒:这段代码一切正常,运行良好,唯一的问题是我不能link 不在模板目录中的 html 文件
我在自己的项目中使用子目录做了类似的事情,对我有用的类似于以下内容:
return render_template('projects/' + link + '/1 . index.html')
如果有帮助请告诉我!