我为我的模型创建了一个视图,其中包含相应的网址和模板文件。然后,在管理面板中,我创建了一个富文本页面,指定了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 :(得分:0)
TemplateResponse不期望其参数中的请求。请参阅the docs。
return TemplateResponse(templates, {'ingredients':ingredients})
但是我希望你打算在那里使用标准render
函数:
return render(request, "pages/ingredients.html", {'ingredients':ingredients})
答案 1 :(得分:0)
好的,解决方案已经在我的项目urls.py文件中的任何其他定义之前定义了我的应用程序URL。
<强> 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)),
)