Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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:通过url设置上下文';让我们_Django_Django Context - Fatal编程技术网

Django:通过url设置上下文';让我们

Django:通过url设置上下文';让我们,django,django-context,Django,Django Context,如何通过base.html根据url执行特定操作? 我在base.html中有两个if子句作为上下文语句。如果GET中有代数,那么应该显示给定的上下文 我的url.conf 伪代码中的My base.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en"> <body> {% if algebra %}

如何通过base.html根据url执行特定操作?

我在base.html中有两个if子句作为上下文语句。如果GET中有代数,那么应该显示给定的上下文

我的url.conf 伪代码中的My base.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <body>
              {% if algebra %}                                    
                  <div>... -- do this -- </div> 
              {% endif %}

              {% if math %}
                  <div>... -- do this -- </div>
              {% endif %}

{%if代数%}
... -- 这样做--
{%endif%}
{%if math%}
... -- 这样做--
{%endif%}

您不显示视图函数,但这里有一个简单的结构:

def algebra(request):
    return common_view(request, algebra=True)

def math(request):
    return common_view(request, math=True)

def common_view(request, math=False, algebra=False):
    ctx = Context()
    ctx['algebra'] = algebra
    ctx['math'] = math
    # blah blah, all the other stuff you need in the view...
    return render_to_response("base.html", ctx)

(我可能有一些打字错误,这是我不知道的)。

您不显示视图功能,但这里有一个简单的结构:

def algebra(request):
    return common_view(request, algebra=True)

def math(request):
    return common_view(request, math=True)

def common_view(request, math=False, algebra=False):
    ctx = Context()
    ctx['algebra'] = algebra
    ctx['math'] = math
    # blah blah, all the other stuff you need in the view...
    return render_to_response("base.html", ctx)

(我可能会有一些打字错误,这太离谱了)。

Ned基于变量值的方法的另一种选择是使用两个不同的模板来扩展公共基础模板。例如

在templates/base.html中:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <body>
      {% block main_body %}                                    
      {% endblock main_body %}                                    
       etc., etc.
做一些类似的数学题。每种方法都有优点和缺点。选择一个最适合你的问题

更新:
“algebra.html”
作为第一个参数传递给
render\u to\u response()
。它扩展了
“base.html”
,除了显式替换的块之外,它使用了所有的base.html。有关如何工作的说明,请参见。模板继承是一个非常强大的概念,用于在大量页面上实现一致的外观和感觉,这些页面的正文不同,但共享部分或全部菜单等。您可以执行多级模板继承,这非常适合管理具有与“主要外观”,但希望尽可能多地共享HTML/CSS


这是模板世界中的一个关键原则。

Ned基于变量值的方法的替代方法是使用两个不同的模板来扩展公共基础模板

在templates/base.html中:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <body>
      {% block main_body %}                                    
      {% endblock main_body %}                                    
       etc., etc.
对于
数学
或其他任何方法,也要做类似的事情。每种方法都有优点和缺点。选择一种最适合你的问题的方法

更新:您将
“algebra.html”
作为第一个参数传递给
render\u to\u response()
。它扩展了
“base.html”
,因为它使用除块之外的所有参数模板继承是一个非常强大的概念,用于在大量页面上实现一致的外观和感觉,这些页面的正文不同,但共享部分或全部菜单等。您可以执行多级模板继承,这对于管理站点非常好hat具有与“主外观”有显著差异的子部分,但希望尽可能多地共享HTML/CSS


这是模板世界中的一个关键原则。

您的视图方法是什么?它是
def common\u view(request):ctx=Context();return\u to\u response(“base.html”,ctx)
?您的视图方法是什么?它是
def common\u view(request):ctx=Context();return\u to\u response(“base.html”,ctx)