编辑 main.html 以引用静态网页。

huangapple go评论59阅读模式
英文:

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.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;title&gt;B. Rabbit&lt;/title&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;

    &lt;h3&gt;Applications&lt;/h3&gt;

    &lt;ul&gt;
        &lt;li&gt;&lt;p&gt;&lt;a href=&quot;/polls&quot;&gt;A Polls Application.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
        &lt;li&gt;&lt;p&gt;&lt;a href=&quot;/hello&quot;&gt;Hello World App.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
        &lt;li&gt;&lt;p&gt;&lt;/p&gt;&lt;a href=&quot;/HWFolder/HWPage.htm&quot;&gt;Hello World stand-alone page.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
    &lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;

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 &quot;site&quot; content
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.join(BASE_DIR, &#39;site&#39;)


urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls),
    path(&#39;polls/&#39;, include(&#39;polls.urls&#39;)),
    path(&#39;hello/&#39;, include(&#39;hello.urls&#39;)),
]

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页面中添加链接而不创建单独的应用程序,请按照以下步骤进行:

  1. 在Django项目的根目录(与manage.py文件位于同一级别)中创建一个名为“static”的文件夹。

  2. 在“static”文件夹内创建另一个名为“HWFolder”的文件夹。

  3. 将您的HTML文件“HWPage.htm”放置在“HWFolder”文件夹内。

  4. 在Django项目的urls.py文件中,在顶部添加以下导入:

    from django.views.generic.base import TemplateView
    
  5. 修改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')),
    ]
    
  6. 保存更改并运行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:

  1. Create a folder called "static" in the root directory of your Django project (the same level as the manage.py file).

  2. Inside the "static" folder, create another folder called "HWFolder".

  3. Place your HTML file "HWPage.htm" inside the "HWFolder".

  4. In your Django project's urls.py file, add the following import at the top:

    from django.views.generic.base import TemplateView
    
  5. Modify the urlpatterns list to include a new path for serving the static HTML file:

    urlpatterns = [
        path(&#39;admin/&#39;, admin.site.urls),
        path(&#39;polls/&#39;, include(&#39;polls.urls&#39;)),
        path(&#39;hello/&#39;, include(&#39;hello.urls&#39;)),
        path(&#39;HWFolder/HWPage.htm&#39;, TemplateView.as_view(template_name=&#39;HWFolder/HWPage.htm&#39;)),
    ]
    
  6. 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 = &#39;/static/&#39;
STATIC_ROOT = os.path.join(BASE_DIR, &#39;staticfiles&#39;)

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.

huangapple
  • 本文由 发表于 2023年5月28日 15:18:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定