2017-09-09 4 views
0

対応するURLとテンプレートファイルを使用してモデルのビューを作成しました。次に、管理パネルで、urlpatternsで定義されている同じURL(成分)を指定してリッチテキストページを作成しました。メザニンはビューを無視し、テンプレートを表示しますが、コンテキストを渡しません。 どうすれば解決できますか?メザニンはビューを無視してテンプレートを表示しますが、コンテキストは渡しません

これらはコードである:

models.py

from django.db import models 
from mezzanine.pages.models import Page 
from django.utils.translation import ugettext_lazy as _ 

class Ingredient(Page): 
    name = models.CharField(max_length=60) 
    information = models.TextField(null=True, blank=True, verbose_name=_("Description")) 

views.py

from django.template.response import TemplateResponse 
from .models import Ingredient 

def ingredients(request): 
    ingredients = Ingredient.objects.all().order_by('name') 
    templates = ["pages/ingredients.html"] 
    return TemplateResponse(request, templates, {'ingredients':ingredients}) 

urls.py

from django.conf.urls import url 
from .views import ingredients 

urlpatterns = [ 
    url("^$", ingredients, name="ingredients"), 
] 

答えて

0

TemplateResponseは、その引数に要求を期待していません。 the docsを参照してください。私のプロジェクトのurls.pyファイル内の他の定義の前に私のアプリのURLを定義

return render(request, "pages/ingredients.html", {'ingredients':ingredients}) 
+0

それから私は私が試してみたかった、ブログ、ギャラリーアプリケーションを見ていたと、彼らはTemplateResponseを使用することを見て。実際には、どちらか一方の方法では機能しません。 django.shortcutsは.modelsから 'render' をインポートから' をインポートIngredient' 'defの成分(リクエスト)::' '成分= Ingredient.objects.all は、これは私がレンダリングを使用して書かれていたコードでした – Rainell

+0

正しい表示が使用されていることを確認してください。私は実際にあなたの最初の試みがエラーを起こすことを期待していました。 –

+0

私はレンダリング版に戻っていますが、何のエラーもありません。私はビューが実行されないと確信していますが、私はなぜか分からないのですか?メザニンは、ビューが存在しないかのように動作します。コードに問題はないと思っています。それ以外の場合、djangoはエラーを返します。構成や構造に問題があるかもしれませんが、私はそれが何であるか分かりません。 – Rainell

0

[OK]を、解決策がされています

return TemplateResponse(templates, {'ingredients':ingredients}) 

は、しかし、私はあなたがそこに標準render機能を使用するためのものを期待しています。

PROJECT_NAME/PROJECT_NAME/urls.py

# Add the urlpatterns for any custom Django applications here. 
# You can also change the ``home`` view to add your own functionality 
# to the project's homepage. 

urlpatterns = [ 
    url(r'^ingredients/', include("apps.ingredients.urls")), 
] 

urlpatterns += i18n_patterns(
    # Change the admin prefix here to use an alternate URL for the 
    # admin interface, which would be marginally more secure. 
    url("^admin/", include(admin.site.urls)), 
) 
関連する問題