英文:
Django : Edit main.html to reference static webpage
问题
我正在学习Django,对它还很陌生,所以我还不太明白所有的部分是如何配合的。
我已成功在教程网站(https://docs.djangoproject.com/en/4.2/intro/tutorial01/)上构建了投票应用程序以及一个额外的“Hello World!”应用程序。我在根目录下创建了一个主页,其中包含指向这两个应用程序的链接。以下是此代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>B. Rabbit</title>
<meta charset="UTF-8">
</head>
<body>
<h3>Applications</h3>
<ul>
<li><p><a href="/polls">A Polls Application.</a></p></li>
<li><p><a href="/hello">Hello World App.</a></p></li>
<li><p><a href="/HWFolder/HWPage.htm">Hello World stand-alone page.</a></p></li>
</ul>
</body>
</html>
我现在想在我的项目中创建一个新的文件夹,其中包含一个静态网页(只是一个简单的HTML文件),并在我的主页上添加一个指向此静态网页的链接。也就是说,不创建一个python manage.py startapp hello
应用程序,而只是创建一个.html
文件,将其放在某个文件夹中,然后指向它。但我不知道如何做到这一点。
上面的第三个列表对象是我的尝试,但这会导致404页面未找到错误。
以下是网站的urls.py
脚本。我通过遵循投票应用程序的语法成功使Hello World应用程序工作,但我不知道如何编辑它来引用一个独立的页面。
import os
from django.contrib import admin
from django.urls import include, path, re_path
from django.views.static import serve
from django.views.generic.base import TemplateView
# 向上两个文件夹以提供“site”内容
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.join(BASE_DIR, 'site')
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
path('hello/', include('hello.urls')),
]
有人能否指导我在哪些脚本中进行编辑,或者如何调整网站的urls.py
以引用一个独立页面,比如在文件夹/HWFolder
中的HWPage.htm
?
英文:
I am learning Django and I am still very new to it so I don't yet understand how all the pieces fit together.
I have successfully built the polls application on the tutorial website (https://docs.djangoproject.com/en/4.2/intro/tutorial01/) and a 'Hello World!' app as an additional test. I have created a main page at the root with links to these two applications. Code for this is below.
<!DOCTYPE html>
<html lang="en">
<head>
<title>B. Rabbit</title>
<meta charset="UTF-8">
</head>
<body>
<h3>Applications</h3>
<ul>
<li><p><a href="/polls">A Polls Application.</a></p></li>
<li><p><a href="/hello">Hello World App.</a></p></li>
<li><p></p><a href="/HWFolder/HWPage.htm">Hello World stand-alone page.</a></p></li>
</ul>
</body>
</html>
I now want to create a new folder in my project with a static webpage (just a simple html file) and add a link to my main page that will point to this static webpage. That is, rather the create an app via python manage.py startapp hello
, I want to just create a raw .html
file, stick it in a folder somewhere, and then point to this. But I don't know how to do this.
The third list object above is my attempt, but this produces a 404 Page not found error.
Below is the urls.py
script for the website. I was able to get the Hello World app working by following the syntax for the polls app, but I do not know how to edit this to just refer to a stand-alone page.
import os
from django.contrib import admin
from django.urls import include, path, re_path
from django.views.static import serve
from django.views.generic.base import TemplateView
# Up two folders to serve "site" content
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.join(BASE_DIR, 'site')
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
path('hello/', include('hello.urls')),
]
Could someone point me in the right direction on what scripts I need to edit or how I adjust the site's urls.py
to refer to a stand-alone page, let's say HWPage.htm
in the folder /HWFolder
.
答案1
得分: 1
要在Django中的独立HTML页面中添加链接而不创建单独的应用程序,请按照以下步骤进行:
-
在Django项目的根目录(与manage.py文件位于同一级别)中创建一个名为“static”的文件夹。
-
在“static”文件夹内创建另一个名为“HWFolder”的文件夹。
-
将您的HTML文件“HWPage.htm”放置在“HWFolder”文件夹内。
-
在Django项目的
urls.py
文件中,在顶部添加以下导入:from django.views.generic.base import TemplateView
-
修改
urlpatterns
列表以包括用于提供静态HTML文件的新路径:urlpatterns = [ path('admin/', admin.site.urls), path('polls/', include('polls.urls')), path('hello/', include('hello.urls')), path('HWFolder/HWPage.htm', TemplateView.as_view(template_name='HWFolder/HWPage.htm')), ]
-
保存更改并运行Django开发服务器。
现在,当您访问URL /HWFolder/HWPage.htm
时,Django将从静态文件夹中提供“HWPage.htm”文件。
确保您的Django项目的settings.py
文件包含以下用于提供静态文件的配置:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
这允许Django从“static”文件夹中找到并提供静态文件。
记得重新启动Django开发服务器以使更改生效。
英文:
To add a link to a standalone HTML page in Django without creating a separate app, you can follow these steps:
-
Create a folder called "static" in the root directory of your Django project (the same level as the manage.py file).
-
Inside the "static" folder, create another folder called "HWFolder".
-
Place your HTML file "HWPage.htm" inside the "HWFolder".
-
In your Django project's
urls.py
file, add the following import at the top:from django.views.generic.base import TemplateView
-
Modify the
urlpatterns
list to include a new path for serving the static HTML file:urlpatterns = [ path('admin/', admin.site.urls), path('polls/', include('polls.urls')), path('hello/', include('hello.urls')), path('HWFolder/HWPage.htm', TemplateView.as_view(template_name='HWFolder/HWPage.htm')), ]
-
Save the changes and run your Django development server.
Now, when you visit the URL /HWFolder/HWPage.htm
, Django will serve the "HWPage.htm" file from the static folder.
Make sure that your Django project's settings.py
file includes the following configuration for serving static files:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
This allows Django to find and serve static files from the "static" folder.
Remember to restart the Django development server for the changes to take effect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论