Django我的模型没有从一个文件到另一个文件

时间:2016-07-05 18:48:31

标签: django listview django-models django-urls detailview

我要将我的菜单项显示在ListView页面上,但是当我点击链接时,没有任何数据显示正确。

这是我的menu-carousel.html

    {% for item in object_list %}
   <li>
      <div class="impx-menu-page-item">
        <div class="impx-menu-page-content">
          <h4><a href="/menu/{{ item.id }}">{{ item.title }}</a></h4>
             <div class="impx-menu-page-price">
              <h5>${{ item.price }}</h5>
             </div>
             <p>{{ item.description }}</p>
        </div>
      </div>
   </li>
  {% endfor %}

这是有效的,现在这里是破碎的部分,当我到/ menu / 1或/ menu /'无论数字'

<div class="container">
        <div class="row">
            <h3><a href="/menu/{{ item.id }}"></a>{{ item.title }}</h3>

        </div>
        <div class="row">
            <div class = "col-sm-12 col-md-4">
                <img src="{{ item.image.url }}" height="300px" width="300px" class = "img img-responsive thumbnail"/>
            </div>
            <div class = "col-sm-12 col-md-4">
                <h6>Price: ${{ item.price }}</h6>
                <h6>Materials: {{ item.description }}</h6>
            </div>
            <div class = "col-sm-12 col-md-4">
                <p>{{ item.description|safe|linebreaks }}</p><br /><br />
                <hr />
                <h5></h5>

            </div>
            <br><br>
        </div>
    </div>

这是我浏览器中显示的内容

<div class="container">
    <div class="row">
        <h3><a href="/menu/"></a></h3>

    </div>
    <div class="row">
        <div class = "col-sm-12 col-md-4">
            <img src="" height="300px" width="300px" class = "img img-responsive thumbnail"/>
        </div>
        <div class = "col-sm-12 col-md-4">
            <h6>Price: $</h6>
            <h6>Materials: </h6>
        </div>
        <div class = "col-sm-12 col-md-4">
            <p><p></p></p><br /><br />
            <hr />
            <h5></h5>

        </div>
        <br><br>
    </div>
</div>

这是我的urls.py

from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from django.contrib import admin
from . import views
from home.models import Menu


urlpatterns = [
    url(r'^$', views.home, name="home"),
    url(r'^menu/$', ListView.as_view(
                        queryset=Menu.objects.all().order_by("-title")[:25],
                        template_name="menu-carousel.html")),
    url(r'^menu/(?P<pk>\d+)/$', DetailView.as_view(
                        model = Menu,
                        template_name="menu-item.html")),
]

这是我的models.py

from django.db import models
from django.conf import settings

class Menu(models.Model):
    title = models.CharField(max_length = 140)
    price = models.IntegerField()
    description = models.TextField()
    image = models.ImageField(upload_to = 'media/' )
    menu = models.CharField(max_length = 10)

    def __str__(self):
        return self.title

1 个答案:

答案 0 :(得分:0)

我想您在DetailView模板中使用了错误的上下文对象名称。更多细节链接:

https://docs.djangoproject.com/es/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_context_object_name

尝试使用{{menu.id}}而不是{{item.id}}显示菜单项ID。您还可以使用django调试模板查看所有上下文对象。