Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Django模型中的数据在我的主页上显示图像_Python_Django - Fatal编程技术网

Python 使用Django模型中的数据在我的主页上显示图像

Python 使用Django模型中的数据在我的主页上显示图像,python,django,Python,Django,希望简单地显示通过Admin在UL标记之间的模板文件中输入的模型数据中的多张图片。我无法渲染数据以显示图像。我不需要在URL.py中发送任何东西,只需要先污染我主页上的图像。有人能帮我解决问题吗?谢谢大家! 型号.py class Product(models.Model): name = models.CharField(max_length=254, blank=True, null=True) description = models.TextField(blank=Tru

希望简单地显示通过
Admin
UL
标记之间的模板文件中输入的模型数据中的多张图片。我无法渲染数据以显示图像。我不需要在
URL.py中发送任何东西,只需要先污染我主页上的图像。有人能帮我解决问题吗?谢谢大家!

型号.py

class Product(models.Model):
    name = models.CharField(max_length=254, blank=True, null=True)
    description = models.TextField(blank=True, null=True)    
    color_name = models.CharField(max_length=254, null=True, blank=True)
    size_types = models.CharField(max_length=7, null=True, blank=True)
    product_price = models.DecimalField(max_digits=9,decimal_places=2)
    old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00) #To show original price if, new price has been added
    product_tags = models.CharField(max_length=254, null=True, blank=True, help_text='Comma-delimited set of SEO keywords for product tag area')
    novelty = models.CharField(max_length=254, null=True, blank=True)
    product_website = models.URLField(max_length=200,  null=True, blank=True) #To show other sites to Users, where they can purchase the particular product
    image = models.ImageField(upload_to='images/products/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified
    slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.')

  #This shows when each item was uploaded & by who, to the User 
    uploaded_by = models.CharField(max_length=254, blank=True, null=True)
    uploaded_at = models.DateTimeField(auto_now=True)

  #For Admin Purposes, to track and see which if still active by for administrative users only
    is_active = models.BooleanField(default=True)

    #Foreign Keys & other relationships
    designer = models.ForeignKey(Designer)
    boutique = models.ForeignKey(Boutique)
    category = models.ForeignKey(ProductCategory)

    #Metadata
    class Meta:
        verbose_name = _("Product")
        verbose_name_plural = _("Products")

    #Helps return something meaningful, to show within the admin interface for easy interaction
    def __unicode__(self):
        return "{0}".format(self.name)
from __future__ import unicode_literals

from django import forms

from django.forms import extras, ModelForm

from products.models import Designer, Product, ProductCategory, Boutique

class DesignerForm(ModelForm):
    class Meta:
        model = Designer

class ProductForm(ModelForm):
    class Meta:
        model = Product

class BoutiqueForm(ModelForm):
    class Meta:
        model = Boutique

class ProductCategoryForm(ModelForm):
    class Meta:
        model = ProductCategory
from __future__ import unicode_literals

from django.http import Http404, HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.generic import DetailView

from django.contrib import auth, messages
from django.contrib.sites.models import get_current_site
from django.shortcuts import render

from products.forms import ProductForm, ProductCategoryForm
from products.forms import BoutiqueForm
from products.forms import DesignerForm

from products.models import Boutique, Product, ProductCategory, Designer


class ProductView(DetailView):
    model = Product
    context_object_name = "task"
Forms.py

class Product(models.Model):
    name = models.CharField(max_length=254, blank=True, null=True)
    description = models.TextField(blank=True, null=True)    
    color_name = models.CharField(max_length=254, null=True, blank=True)
    size_types = models.CharField(max_length=7, null=True, blank=True)
    product_price = models.DecimalField(max_digits=9,decimal_places=2)
    old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00) #To show original price if, new price has been added
    product_tags = models.CharField(max_length=254, null=True, blank=True, help_text='Comma-delimited set of SEO keywords for product tag area')
    novelty = models.CharField(max_length=254, null=True, blank=True)
    product_website = models.URLField(max_length=200,  null=True, blank=True) #To show other sites to Users, where they can purchase the particular product
    image = models.ImageField(upload_to='images/products/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified
    slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.')

  #This shows when each item was uploaded & by who, to the User 
    uploaded_by = models.CharField(max_length=254, blank=True, null=True)
    uploaded_at = models.DateTimeField(auto_now=True)

  #For Admin Purposes, to track and see which if still active by for administrative users only
    is_active = models.BooleanField(default=True)

    #Foreign Keys & other relationships
    designer = models.ForeignKey(Designer)
    boutique = models.ForeignKey(Boutique)
    category = models.ForeignKey(ProductCategory)

    #Metadata
    class Meta:
        verbose_name = _("Product")
        verbose_name_plural = _("Products")

    #Helps return something meaningful, to show within the admin interface for easy interaction
    def __unicode__(self):
        return "{0}".format(self.name)
from __future__ import unicode_literals

from django import forms

from django.forms import extras, ModelForm

from products.models import Designer, Product, ProductCategory, Boutique

class DesignerForm(ModelForm):
    class Meta:
        model = Designer

class ProductForm(ModelForm):
    class Meta:
        model = Product

class BoutiqueForm(ModelForm):
    class Meta:
        model = Boutique

class ProductCategoryForm(ModelForm):
    class Meta:
        model = ProductCategory
from __future__ import unicode_literals

from django.http import Http404, HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.generic import DetailView

from django.contrib import auth, messages
from django.contrib.sites.models import get_current_site
from django.shortcuts import render

from products.forms import ProductForm, ProductCategoryForm
from products.forms import BoutiqueForm
from products.forms import DesignerForm

from products.models import Boutique, Product, ProductCategory, Designer


class ProductView(DetailView):
    model = Product
    context_object_name = "task"
视图.py

class Product(models.Model):
    name = models.CharField(max_length=254, blank=True, null=True)
    description = models.TextField(blank=True, null=True)    
    color_name = models.CharField(max_length=254, null=True, blank=True)
    size_types = models.CharField(max_length=7, null=True, blank=True)
    product_price = models.DecimalField(max_digits=9,decimal_places=2)
    old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00) #To show original price if, new price has been added
    product_tags = models.CharField(max_length=254, null=True, blank=True, help_text='Comma-delimited set of SEO keywords for product tag area')
    novelty = models.CharField(max_length=254, null=True, blank=True)
    product_website = models.URLField(max_length=200,  null=True, blank=True) #To show other sites to Users, where they can purchase the particular product
    image = models.ImageField(upload_to='images/products/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified
    slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.')

  #This shows when each item was uploaded & by who, to the User 
    uploaded_by = models.CharField(max_length=254, blank=True, null=True)
    uploaded_at = models.DateTimeField(auto_now=True)

  #For Admin Purposes, to track and see which if still active by for administrative users only
    is_active = models.BooleanField(default=True)

    #Foreign Keys & other relationships
    designer = models.ForeignKey(Designer)
    boutique = models.ForeignKey(Boutique)
    category = models.ForeignKey(ProductCategory)

    #Metadata
    class Meta:
        verbose_name = _("Product")
        verbose_name_plural = _("Products")

    #Helps return something meaningful, to show within the admin interface for easy interaction
    def __unicode__(self):
        return "{0}".format(self.name)
from __future__ import unicode_literals

from django import forms

from django.forms import extras, ModelForm

from products.models import Designer, Product, ProductCategory, Boutique

class DesignerForm(ModelForm):
    class Meta:
        model = Designer

class ProductForm(ModelForm):
    class Meta:
        model = Product

class BoutiqueForm(ModelForm):
    class Meta:
        model = Boutique

class ProductCategoryForm(ModelForm):
    class Meta:
        model = ProductCategory
from __future__ import unicode_literals

from django.http import Http404, HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.generic import DetailView

from django.contrib import auth, messages
from django.contrib.sites.models import get_current_site
from django.shortcuts import render

from products.forms import ProductForm, ProductCategoryForm
from products.forms import BoutiqueForm
from products.forms import DesignerForm

from products.models import Boutique, Product, ProductCategory, Designer


class ProductView(DetailView):
    model = Product
    context_object_name = "task"
模板

{% extends "site_base.html" %}

{% load i18n %}

{% block body %}
    <div id="main" role="main">
        <ul id="tiles">
            <li>
                {% for task in products %}
                    <img src="{{MEDIA_URL}}images/product/main {{task.image.url}}" />
                  {% endfor %}
            </li>         
         </ul>
    </div>
{% endblock %}
{%extends“site_base.html”%}
{%load i18n%}
{%block body%}
  • {%用于产品%中的任务} {%endfor%}
{%endblock%}
这段代码中有一些奇怪的东西

首先,你想对所有的产品做点什么。那么,为什么要使用DetailView来选择和显示单个项目呢?您需要使用ListView,它将传递产品列表


第二,出于某种原因,您将
上下文\u对象\u名称
重写为“任务”。但是,在模板中,您迭代“产品”——一个没有提供的名称。如果您已将上下文对象称为“任务”,那么您应该迭代它。

了解我做错了什么

显然,我需要进入我的url并更改第一个
url元组中已经存在的
TemplateView

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import TemplateView

from django.contrib import admin

urlpatterns = patterns('',
url(r"^$", TemplateView.as_view(template_name="homepage.html"), name="home"),

url(r'^admin/', include(admin.site.urls)),
url(r"^account/", include("account.urls")),
url(r'^likes/', include('likes.urls')),
)

  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
并将
URL元组
更改为
列表视图
,以从我的django模型生成图像

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import ListView
from products.models import Product

from django.contrib import admin


urlpatterns = patterns('',
    url(r"^$", ListView.as_view(
        template_name="homepage.html",
        model = Product,
        context_object_name = "products",
        ), name="home"),

    url(r'^admin/', include(admin.site.urls)),
    url(r"^account/", include("account.urls")),
    url(r'^likes/', include('likes.urls')),
)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

谢谢你的更正!基于这个建议,我能够解决这个问题!