Ste*_*err 5 html django django-templates django-rest-framework
我想自定义 Django REST Framework 可浏览 API 模板(只需将品牌更改为不同的名称和链接)。
我已阅读有关如何实现此目标的文档,并最初在以下路径中执行了以下操作:hints(project)->hints1(app)->templates->rest_framework->api.html
api.html:
{% extends "rest_framework/base.html" %}
{% block title %} Handy Dev Hints - API {% endblock %}
{% block branding %}
<span>
<a class='navbar-brand' rel="nofollow" href="{% url 'html' %}">
-----HTML View----- <span class="version">1</span>
</a>
</span>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
我还修改了我的settings.py,如下所示,特别是 DIRS 部分:
设置.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
从我看过的教程和我读过的文档来看,这应该足以实现更改。然而,这并没有奏效。
所以我决定直接在 site-packages 库中更改 base.html 。
基本.html:
<!DOCTYPE html>
.
.
.
{% block body %}
<body class="{% block bodyclass %}{% endblock %}">
<div class="wrapper">
{% block navbar %}
<div class="navbar navbar-static-top {% block bootstrap_navbar_variant %}navbar-inverse{% endblock %}"
role="navigation" aria-label="{% trans "navbar" %}">
<div class="container">
<span>
{% block branding %}
<a class='navbar-brand' rel="nofollow" href="{% url 'html' %}">
-----HTML View-----
</a>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
当我在本地服务器上运行该解决方案时,它起作用了。但是,当我将文件上传到外部服务器时,它不起作用(我也将rest_framework和rest_framework_jwt站点包上传到外部服务器,但我认为我仍然缺少一些东西)。
关于如何按照文档的建议使更改在外部服务器上生效有什么建议吗?或者甚至通过 base.html 更改方法?(或任何其他方法)。
多谢!
1)首先,您似乎在应用程序文件夹中创建了 api.htm 文件,同时在项目目录级别引用模板文件夹,这意味着
[Your app template ref] Proj dir --> app --> templates --> api.html
[django 模板查找] Proj dir --> templates --> api.html导致错误
2)其次,作为替代解决方案,您选择破解站点包安装(非常糟糕!不推荐),而这可以在本地工作,当您在外部服务器上部署应用程序时,DRF将重新安装在外部服务器中(这不会有您所做的任何修改)
1) 将api.htm移至项目级别文件夹 .ie Proj dir --> templates --> api.html
{% extends "rest_framework/base.html" %}
{% block title %} Handy Dev Hints - API {% endblock %}
{% block branding %}
<span>
<a class='navbar-brand' rel="nofollow" href="{% url 'html' %}">
-----HTML View----- <span class="version">1</span>
</a>
</span>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
让 django 轻松找到
或
2)将os.path.join('BASE_DIR', 'YOUR_APP_DIR' , 'templates')添加到设置中的模板目录。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, os.path.join(BASE_DIR, 'YOUR_APP_DIR', 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
如果我可以补充一下,只要所有内容都放在适当的路径中以便框架可以找到,遵循有关如何自定义此内容的教程就可以工作,这是一个缺少我所做的自定义的屏幕。
| 归档时间: |
|
| 查看次数: |
4625 次 |
| 最近记录: |