使用django在应用中加入html文件
时间: 2023-10-22 12:14:25 浏览: 36
可以通过以下步骤在 Django 应用中加入 HTML 文件:
1. 在应用的根目录下创建一个名为 "templates" 的文件夹。
2. 在 "templates" 文件夹中创建一个 HTML 文件,例如 "example.html"。
3. 在 views.py 文件中编写视图函数,该函数将渲染 "example.html" 文件。例如:
```python
from django.shortcuts import render
def example_view(request):
return render(request, 'example.html')
```
4. 在应用的 urls.py 文件中添加一个 URL 映射,将 URL 路径与视图函数相关联。例如:
```python
from django.urls import path
from . import views
urlpatterns = [
path('example/', views.example_view, name='example'),
]
```
5. 确保您在 Django 项目的 settings.py 文件中设置了正确的模板路径。例如:
```python
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['path/to/templates/folder'], # 指定模板文件夹路径
'APP_DIRS': True,
# ...
},
]
```
这样,当用户访问 "/example/" URL 时,Django 将渲染 "example.html" 文件并返回给用户。