2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

direct_to_templateからTemplateViewへ移行できない

Last updated at Posted at 2014-03-27

Djangoチュートリアル(後編)で詰まった点
参考URL: http://codezine.jp/article/detail/4264?p=3

Django 1.6で上記チュートリアルを進めると・・・

# 略
from django.views.generic.simple import direct_to_template
# 略
def item_page_display(request, item_id):
    item = get_object_or_404(Item, id=item_id)
    return direct_to_template(request, 'page/item.html', extra_context = {'item': item })

とすると、direct_to_templateのImportErrorが出るので、
TemplateViewの使用を検討。
参考URL: http://stackoverflow.com/questions/11005733/moving-from-direct-to-template-to-new-templateview-in-django

ところが、
AttributeError: 'function' object has no attribute 'get'が、site-packages/django/middleware/clickjacking.py内のprocess_responseで起こってよーわからんくなったので、
direct_to_templateに似た働きをするrenderやrender_to_responseをインポートして使用。

# 略
from django.shortcuts import render, render_to_response
from django.template import RequestContext
# 略
def item_page_display(request, item_id):
    item = get_object_or_404(Item, id=item_id)
    return render_to_response('page/item.html', {'item':item}, context_instance=RequestContext(request))
    # または、よりシンプルに、return render(request, 'page/item.html', {'item': item})

参考URL: https://groups.google.com/forum/#!topic/django-users/ZuDi-iqd1Xk

これで一応商品画面が表示されるようになった。

shot.png

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?