Django REST可浏览API模板更改 [英] Django REST Browsable API Template Change

查看:61
本文介绍了Django REST可浏览API模板更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义Django REST Framework可浏览API模板(只需将商标更改为其他名称和链接)。

I want to customize the Django REST Framework Browsable API template (simply to change the branding to a different name and link).

我已经阅读了有关如何实现此目的的文档,并最初在以下路径中进行了以下操作:hints(project)-> hints1(app)-> templates-> rest_framework-> api.html

I have read the documentation on how to achieve this and did the following initially in the following path: 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 %}

我还如下修改了settings.py,特别是DIRS部分:

I also modified my settings.py as follows, specifically the DIRS section:

settings.py:

settings.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',
        ],
    },
},
]

从我看过的教程和我的文档中阅读本文应该足以实施更改。但是,它没有用。

From the tutorials I've watched and the docs I read this should have been enough to implement the change. However, it did not work.

因此,我决定直接在site-packages库中更改base.html。

So then I decided to just change the base.html directly in the site-packages library.

base.html:

base.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 %}

此解决方案在本地服务器上运行时有效。但是,当我将文件上传到外部服务器时,它不起作用(我上传了 rest_framework rest_framework_jwt 网站-包也可以连接到外部服务器,但我想我仍然缺少某些东西)。

This solution worked when I ran it on my local server. However, it did not work when I uploaded the files to an external server (I uploaded the rest_framework and rest_framework_jwt site-packages to the external server also, but I assume I'm still missing something).

关于文档中建议如何使更改在外部服务器上工作的任何建议?甚至通过base.html更改方法? (或任何其他方法)。

Any advice on how to get the changes to work on the external server as suggested by the documentation? Or even via the base.html change method? (Or any other method).

非常感谢!

推荐答案

有两件事可能是您遇到此问题的原因:



1)首先,您似乎在应用中创建了api.htm文件文件夹,同时在项目目录级别引用模板文件夹,这意味着

[您的应用程序模板引用] 项目目录->应用程序->模板-> api。 html

[django模板查找] 项目目录->模板-> api.html 错误

There are a couple of things that could likely be the reason you are experiencing this problem:

1) Firstly you seem to have your api.htm file created in your app folder while you reference your templates folder at the level of the project directory meaning
[Your app template ref] Proj dir --> app --> templates --> api.html
[django template lookup] Proj dir --> templates --> api.html which result to error

2)第二次作为替代解决方案,您选择破解站点程序包安装(非常糟糕!不推荐),而这会在本地工作,当您在外部服务器上部署应用程序时,DRF将重新安装在外部服务器上(此操作不会进行任何修改)

2) Secondly as alternative solution you chose to hack the site-package installation(very bad!! not recommend) while this would work locally, when you deploy your application on an external server, the DRF is reinstall in the external server (which this will not have any modifications you made)

1)将 api.htm 移动到项目级别的文件夹。 项目目录->模板-> 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 %}

以便django轻松找到
OR

2)您添加的 os.path.join('BASE_DIR','YOUR_APP_DIR','templates')到设置中的模板目录。即

for django to easily find
OR
2) Your add os.path.join('BASE_DIR','YOUR_APP_DIR', 'templates') to your template dir in the settings .i.e.

    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',
        ],
    },
},
]



更新:



如果我可以添加的话,遵循有关如何自定义的教程将一直有效因为所有内容都放置在框架可以找到的适当路径内,所以这里缺少我所做的自定义。

Update:

And if i may add, following the tutorial on how to customise this will work as long as everything is place within the appropriate path such that the framework can find, here is a screen short of a customisation I made.

这篇关于Django REST可浏览API模板更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆