无法从数据库呈现Django模板

时间:2019-06-13 07:49:57

标签: django django-templates django-class-based-views

我正在使用基于类的视图,到目前为止一切顺利。 最近,我遇到了一些我无法理解的逻辑或代码相关问题。

我有一个html文件,我想在其中传递或显示两个模型: PhotographyDataPictures。我可以成功使用两个不同的ListView传递模型。但是Pictures ListView不会显示任何数据,但是会显示对象ID。

类似地,我尝试创建一个PicturesDetailView来检查数据库是否没有引起任何错误,即使在DetailView中也没有任何数据或ID传递。

views.py

class PhotographyListView(ListView):
    model = PhotographyData
    template_name = "projectfiles/PhotographyView.html"

    def get_queryset(self):
        return PhotographyData.objects.all()


class PicturesListView(ListView):
    model = Pictures
    template_name = "projectfiles/PhotographyView.html"
    queryset = Pictures.objects.all()


class PicturesDetail(DetailView):
    model = Pictures
    template_name = 'projectfiles/View.html'

models.py

class PhotographyData(models.Model):
    photography_pkg_name = models.CharField(
        "Photography package", max_length=50)
    photography_pic = models.ImageField(
        upload_to='images', blank=False, null=False)
    photography_price = models.SmallIntegerField(blank=False, null=False)


class Pictures(models.Model):
    pic_name = models.CharField(max_length=50)
    pic_caption = models.TextField()
    date = models.DateTimeField(auto_now=False, auto_now_add=False)
    location = models.CharField(max_length=50)
    pic = models.ImageField(upload_to = 'Pictures', blank=False, null=False)

forms.py

class PhotographyForm(forms.ModelForm):

#overriding the required attribute
    photography_pic = forms.ImageField(required=True)

    class Meta:
        model = PhotographyData
        fields = ("photography_pkg_name",
                  "photography_price", "photography_pic")


class PicturesForm(forms.ModelForm):
    # date = forms.DateField(
    #     widget=forms.DateInput(format=('%m/%d/%y'),
    #                            attrs={'class': 'form-control',
    #                                   'placeholder': ' Month/Date/Year'}))

    class Meta:
        model = Pictures
        fields = ("pic_name", "pic_caption",
                  "date", "location", "pic")

    widgets = {
        # 'date': forms.DateTimeField(
        #     attrs={'placeholder': "Month/Date/Year:H:M" }
        #     ),

        'pic_caption': forms.Textarea(
            attrs={'class': 'form-control', 'placeholder': 'Caption', 'rows': 3, 'cols': 21})}

    pic = forms.ImageField(required = True)

#'%m/%d/%Y % H: % M'

urls.py

from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls import url
from . import views
from django.views.generic import *
from django.views import generic
from django.contrib.auth import views as auth_views

from .views import (
    HomeView,
    Food_CreateView,
    Decor_CreateView,
    Photography_CreateView,
    FoodListView,
    DecorListView,
    PhotographyListView,
    PicturesCreateView,
    PicturesDetail)

urlpatterns = [
    url(r'^(?i)home/$', HomeView.as_view(), name='Home-Page'),
    url(r'^(?i)addpic/$', Photography_CreateView.as_view(), name='Add-Pic'),
    url(r'^(?i)addabc/$', PicturesCreateView.as_view(), name='Add-Pic'),
    url(r'^(?i)PhotoView/$', PhotographyListView.as_view(), name='Photo-View'),
    url(r'^(?i)(?P<pk>[0-9]+)/$', PicturesDetail.as_view(), name='Pic-View'),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

图片的详细视图

View.html

<title>123</title>
{% include 'projectfiles/base.html' %}

ABC
<div class='collection'>
{% for x in object_list %}
<br>
ID:{{x.id}}<br>
NAME:{{x.pic_name}}<br>
DATE:{{x.date}}<br>
Location:{{x.location}}
Details:{{x.pic_caption}}

<img src="{{ i.pic.url }}" alt='Not found' width="124" height="124"/>
{% endfor %}
</div>

{% endblock asd %}

photography.html

<title>VIEW</title>
{% include 'projectfiles/base.html' %}

{% block asd %}


<div class='collection'>
{% for i in object_list %}

{{i.photography_pkg_name}}
{{i.photography_price}}
<a href=#> <img src="{{ i.photography_pic.url }}" alt='Not found' width="124" height="124"/></a>
{% endfor %}
</div>
<br>
<div class='collection'>

{% for x in object_list %}
ABC<br>
ID:{{x.id}}<br>
{% comment %} ID:<a href={% url 'Pic-View' x.id %}>{{x.id}}</a><br> {% endcomment %}
NAME:{{x.pic_name}}<br>
DATE:{{x.date}}<br>
Location:{{x.location}}
{% endfor %}
</div>


{% endcomment %}

{% endblock asd %}

我正在使用 Django表单将数据保存在数据库中,并且可以正常工作。

没有错误使您难以理解为什么数据无法正确传递。虽然已成功保存并显示在管理部分。

Problem

1 个答案:

答案 0 :(得分:1)

这一切有点混乱。您有一个模板和一个URL,但是您似乎期望以某种方式调用两个视图。这根本不是它的工作原理:一个URL映射到一个视图。当然,多个视图可以使用相同的模板,但不能同时使用。

因为您的URL调用PhotographyListView,所以object_list只会包含PhotographyData的实例。如果还要显示图片列表,则需要将其添加到视图上下文中:

class PhotographyListView(ListView):
    model = PhotographyData
    template_name = "projectfiles/PhotographyView.html"


    def get_context_data(self, **kwargs):
        kwargs['pictures'] = Pictures.objects.all()
        return super().get_context_data(**kwargs)

,现在在模板中,第二个循环可以是{% for x in pictures %}

(尽管,再次,我无法真正理解为什么要使用这两个单独的列表。也许想要在Picture和PhotographyData之间建立某种关系?如果需要,例如,需要一个ForeignKey。)