Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 我有两个数据要渲染_Django_Python 3.x - Fatal编程技术网

Django 我有两个数据要渲染

Django 我有两个数据要渲染,django,python-3.x,Django,Python 3.x,现在我有两个数据要呈现:一个在'base.html'中,另一个在'index.html'中 views.py url.py 及 testurl.py base.html index.html 当我运行'index'和'url.py'时,我想先运行'daily_cs'函数来运行它们,但它们不起作用。好的,昨天晚上我正在编写答案。这是我的代码。 只需添加要在“索引”视图中呈现的字段即可 views.py def index(request): r = Content.objects.filt

现在我有两个数据要呈现:一个在'base.html'中,另一个在'index.html'中

views.py url.py 及

testurl.py base.html index.html
当我运行'index'和'url.py'时,我想先运行'daily_cs'函数来运行它们,但它们不起作用。

好的,昨天晚上我正在编写答案。这是我的代码。 只需添加要在“索引”视图中呈现的字段即可

views.py

def index(request):
    r = Content.objects.filter(is_check=True)
    everyday_cs = random.choice(r)
    articles = LifeArticle.objects.all()
    return render(request, 'index.html', {'articles': articles, 'everyday_cs': everyday_cs})
url.py

urlpatterns = [
    url(r'^$', views.index,),
]
base.html

{{everyday_cs}}
index.html

{% extends "base.html" %}
{% if articles %}
{% for article in articles %}
{{article.title}}
{% endfor %}
{% endif %}
好,有工作,以前不懂。
实际上仅用于在视图“索引”中呈现。

如果您希望在任何模板中使用变量,而不考虑任何视图(如base.html或任何其他模板),则应使用
上下文处理器来传递变量。
它们都不起作用
-请共享错误消息或以任何其他方式澄清发生了什么
render(请求'index.html'…
->
render(请求'myapp/index.html'…
无错误消息,我想在模板中放入'daily_cs'的值当我访问'index.html'时,'base.html'中没有'daily_cs'。当我访问索引时,如果'base.html'中有值,我该怎么办?
{% extends "base.html" %}
{% if articles %}
{% for article in articles %}
{{article.title}}
{% endfor %}
{% endif %}
def index(request):
    r = Content.objects.filter(is_check=True)
    everyday_cs = random.choice(r)
    articles = LifeArticle.objects.all()
    return render(request, 'index.html', {'articles': articles, 'everyday_cs': everyday_cs})
urlpatterns = [
    url(r'^$', views.index,),
]
{{everyday_cs}}
{% extends "base.html" %}
{% if articles %}
{% for article in articles %}
{{article.title}}
{% endfor %}
{% endif %}