2012-01-26 14 views
4

Django Webフレームワークの新機能です。Django:オブジェクト/オブジェクトIDを別のテンプレートに渡す方法

私はすべてのオブジェクトのリストを表示するテンプレートを持っています。私はすべての個々のオブジェクトをリンク(オブジェクトタイトル)としてリストアップし、その特定のオブジェクトのオブジェクトの詳細を示す別のページにリダイレクトしたい。 オブジェクトをリストすることはできますが、オブジェクト/オブジェクトIDを次のテンプレートに転送して詳細を表示することはできません。あなたがrender_to_responseの辞書に渡す変数は

{% for listing in object_list %} 
<!--<li> {{ listing.title }} </li>--> 
<a href="{{ listing.id }}">{{ listing.title}}</a><br> 
{% endfor %} 

するlist.html detail.html

{{ id }} 
+0

これは、URLをビュー関数にマップするファイルです。 –

+0

@burhan私はhrefをget_absolute_urlに変更しましたが、それをクリックしても、次のページにobject.idが表示されず、次に詳細ビューがdefの詳細(request、id):l = list.objectsとして表示されます。すべて(pk = id)、render_to_response( '..../templates/detail.html'、 'l':l)しかし、私はそのページでidを取得できません。 models.pyでは、def get_absolute_url()を追加しました:return "/ listing /%i"%self.idそして最後に私のurls.pyにu​​rl(r '^ listings /(?P \ d +)、direct_to_template、 'template': 'listing_detail。html'})私は何をしているのか教えてください – nirvana

答えて

4

:として

views.py

def list(request): 
    listings = listing.objects.all() 
    return render_to_response('/../templates/listings.html',{'listings':listings}) 

def detail(request, id): 
    #listing = listing.objects.filter(owner__vinumber__exact=vinumber) 
    return render_to_response('/../templates/listing_detail.html') 

とテンプレート変数はテンプレートで終わる。したがって、detailでは、{'listing': MyModel.objects.get(id=vinumber)}のようなものを追加する必要があり、次にテンプレートは{{ listing.id }}と表示されます。しかし、IDが存在しないとハットがクラッシュするので、get_object_or_404を使用する方が良いでしょう。

また、テンプレートは、object_listをループが、ビューはlistingsに渡す - それらのいずれかを使用すると、それは現在も....

を働いているならば、あなたは{% url %} tagを使用しなければならないと述べたものとは異なっている必要がありますし、/get_absolute_urlをお持ちの場合:href="{{ listing.id }}"と直接言うのではなく、href="{% url listing-details listing.id %}"と入力してください。listing-detailsurls.pyのビューの名前です。あなたのモデルにget_absolute_url functionを追加するのは、the permalink decoratorです。​​とすれば、URL構造を変更して見栄えを良くしたり、データベースID以外の属性を使用したりすることが容易になります。

+0

私はhrefをget_absolute_urlに変更しましたが、それをクリックしても次のページにobject.idが表示されません。 ( '..../templates/detail.html'、 'l':l)しかし、私はできません。詳細なビューをdef詳細(request、id)として示します:l = list.objects.all(pk = id)、render_to_responseそのページのIDを取得します。 models.pyでは、def get_absolute_url()を追加しました:return "/ listing /%i"%self.idそして最後に私のurls.pyにu​​rl(r '^ listings /(?P \ d +)、direct_to_template、 'template': 'listing_detail.html'})あなたは何をしているのか教えてください。 – nirvana

+0

@nirvanaあなたの問題は実際にはあなたの 'urls.py'にあります。 !これらの 'url()'呼び出しは、URLパターンをビュー関数に関連付けます。実際には、あなたが書いたビューの代わりに 'direct_to_template'"総称ビュー "を使用しています。これは、上記の 'object_list'と' listings'の名前の違いについても説明します。 'my_app_name.views import detail'から' direct_to_template'を 'detail'に変更し、リストページと同様にしたいと思うでしょう。 – Dougal

+0

thANKSそれは私の望むように正確に動作します – nirvana

0

@permalink decoratorを確認する必要があります。これにより、あなたのモデルにURLパターンと対応するview_functionに基づいたリンクを生成させることができます。例えば

<a href={{ example.get_absolute_url }}>{{ example.name }}</a> 

は、この情報がお役に立てば幸い:

# example model 
class Example(models.Model): 
    name = models.CharField("Name", max_length=255, unique=True) 
    #more model fields here 

    #the permalink decorator with get_absolute_url function 
    @models.permalink 
    def get_absolute_url(self): 
     return ('example_view',(), {'example_name': self.name}) 

#example view 
    def example_view(request, name, template_name): 
     example = get_object_or_404(Example, name=name) 
     return render_to_response(template_name, locals(), 
           context_instance=RequestContext(request)) 

    #example urls config 
     url(r'^(?P<name>[-\w]+)/$', 'example_view', {'template_name': 'example.html'}, 'example_view') 

今、あなたはあなたのテンプレートでこのような何かを行うことができます。あなたの細部の方法で

+0

彼はリンクを作成する方法を尋ねていませんでしたが、詳細テンプレートのオブジェクトデータフィールドを取得する方法はありません。 –

+0

@JamieForrest私はhrefをget_absolute_urlに変更しましたが、それをクリックしても次のページにobject.idが表示されず、次に詳細ビューがdefの詳細(request、id)として表示されます:l = list.objects。すべて(pk = id)、render_to_response( '..../templates/detail.html'、 'l':l)しかし、私はそのページでidを取得できません。 models.pyでは、def get_absolute_url()を追加しました:return "/ listing /%i"%self.idそして最後に私のurls.pyにu​​rl(r '^ listings /(?P \ d +)、direct_to_template、 'template': 'listing_detail.html'})私が行っていることと間違っていることを教えてください – nirvana

0

、ちょうどそうのようなあなたのテンプレートにリストを渡す:あなたの質問は、あなたのurls.py.で更新してください

def detail(request, id): 
    l = listing.objects.get(pk=id) 
    return render_to_response('/../templates/listing_detail.html', {'listing':l}) 
+0

私はこの質問を次のように理解しました: テンプレートから私のビューを呼び出すには、パラメータ? 彼は最初のテンプレート呼び出しで既にリスティングパラメータを渡しているので、彼は行う方法を知っていると思います。 –

関連する問題