繁体   English   中英

从 Django 的视图中将 html 传递给 index.html 模板

[英]Passing html to index.html template from view in Django

这是我在 get_rand.py 中的代码

def okdoo(request):
   context = {'index': "<div> sadsadsadasd </div>"}
   render(request, "index.html", context)

这是我在views.py 中的代码

def index(request):
    okdoo(request)
    return render(request, "index.html")

这是我在 index.html 中的代码:

{{index}}

当我尝试加载它时没有显示任何文字....

试试这个:你错过了一个回报 function

def okdoo(request):
   context = {'index': "<div> sadsadsadasd </div>"}
   return render(request, "index.html", context)

确保在你的settings.py中加入带有基本目录的模板

您可以通过此代码进行操作:

TEMPLATE_DIR = os.path.join(BASE_DIR,'your directory')

然后在你的settings.py中找到它

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        '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',
            ],
        },
    },
]

改变你的观点.py

def okdoo(request):
    context = {'index': "<div> sadsadsadasd </div>"}
    return render(request, "index.html", context)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM