django模板不显示从同一视图发送的更新的上下文数据

时间:2014-12-04 11:16:38

标签: django pagination django-templates django-views

问候Django专家!我在尝试更新django模板中的数据时遇到问题。

我正在尝试在我的django应用程序中实现服务器端分页,为此我使用单个视图联系模型以根据前端返回的值获取数据。

例如: 第一次:没有参数从前端发送,因此返回第一页 下次:前端发送基于其生成用于渲染的新数据的附加数据

我的问题是新数据(第二次及以后)没有被发送到django模板。它仍然显示相同的旧数据(第一次获取)但是我能够在python控制台上打印new_data,只是在视图返回渲染之前的一步。

我是否缺少任何设置/ Django配置,以便在相同视图的同一模板上呈现新数据。我已经花了足够的时间来解决这个问题,但却未能找到解决方案。

查看对我不起作用的代码段:

def get_category_records(request):

    # Logic which generates data for result variable

    pprint.pprint(result)
    return render(request, 'SampleApp/category_list.html', {'category' : result })

示例模板SampleApp / category_list.html

<!--{% autoescape off %}-->
{{ category }}
<!--{% endautoescape %}-->

我可以看到更新&#39;结果&#39; django python控制台中的变量值BUt的变化没有反映在django模板中! 我的项目的Setting.py

"""
Django settings for FalsePositiveProject project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Searching path for django templates
TEMPLATE_DIRS = (os.path.join(BASE_DIR, "FalsePositiveApp/templates/FalsePositiveApp"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '%_d*$-q1ere-2ig!1x0zfq_y&#-n7$6q%m&%)b0a@(p%po)-u^'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'FalsePositiveApp',
)

MIDDLEWARE_CLASSES = (
    '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 = 'FalsePositiveProject.urls'

WSGI_APPLICATION = 'FalsePositiveProject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        # [AVI] : Leaving blank want to use PyMongo directly
        #'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/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/1.6/howto/static-files/

STATIC_URL = '/static/'

0 个答案:

没有答案