python - 如何使用 django 呈现未模板化的 html
问题描述
我一直在关注 Django 的教程,我发现这很好用:
视图.py
from django.shortcuts import render
from django.views.generic.base import View
class Home(View):
def get(self, request, *args, **kwargs):
context = {'message': 'Hello Django!'}
return render(request, "index.html", context=context)
索引.html
<p>{{message}}</p>
但是,如果我将 index.html 更改为阅读
<p>{{message}} And more!</p>
它仍将呈现为“Hello Django!”。
然后我改变
context = {'message': 'Bye Django!'}
在views.py
, 并得到
"Bye Django!"
我尝试了{{message|safe}}
和{%load_widget_tweaks %}
(安装后django-widget-twaks
)之类的东西,但没有任何效果。
难道我只能从context
视图中提供的字符串中加载不是html标签的东西吗?这似乎完全疯了。
设置.py:
"""
Django settings for server project.
Generated by 'django-admin startproject' using Django 2.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'keygoeshere'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
# Application definition
INSTALLED_APPS = [
'dashboard',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'server.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
WSGI_APPLICATION = 'server.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ignyte',
'USER': 'aeftimia',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
解决方案
推荐阅读
- python - 如何让我的 selenium 代码运行得更快?
- javascript - 销毁并重新初始化引导滑块
- java - 杰克逊:@JsonUnwrapped 自定义字段名称和自定义反序列化模块时无法识别的字段
- amazon-web-services - 是否可以在 AWS 中自动创建 AMI?
- pip - 如何在openSUSE Tumbleweed上安装柯南包管理器最正确?
- python - 将ftr文件读入R
- android - 仅限三星的 IllegalStateException
- octobercms - 在 OctoberCMS 中急切加载关系数据
- javascript - 当我从下拉菜单中选择选项时多次调用函数
- npm - 有没有办法自动检查 npm 模块依赖关系并在其中一个更新时重建?