0

base.htmlのURLに基​​づいて特定のアクションを実行するにはどうすればよいですか?

base.htmlのコンテキストステートメントとして2つのif-clausesがあります。GETに代数がある場合は、指定されたコンテキストが表示されます。

私のurl.conf

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^algebra/$', 'algebra'),
    (r'^mathematics/$', 'mathematics'),

)

擬似コードの私の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 %}
4

2 に答える 2

2

ビュー関数は表示されませんが、単純な構造は次のとおりです。

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)

(私の頭の中にあるので、いくつかのタイプミスがあるかもしれません)。

于 2010-01-05T01:13:41.167 に答える