python - “Thing”对象没有属性 'uploads'

标签 python django

大家好,我正在通过一本书“hello web app middle”练习Django,,只是不知道为什么会出现错误“Thing”对象没有属性“uploads”,,,这是我的views.py:

from django.shortcuts import render,redirect
from django.contrib.auth.decorators import login_required
from django.http import Http404
from collection.forms import ThingForm,ContactForm
from collection.models import Thing,Upload 
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context


def index(request):
    things = Thing.objects.all()
    return render(request, 'index.html', {
        'things':things,
        })


def thing_detail(request, slug):
    thing = Thing.objects.get(slug=slug)
    social_accounts = thing.social_accounts.all()
    uploads = thing.uploads.all()
    return render(request, 'things/thing_detail.html',{
        'thing':thing,
        'social_accounts': social_accounts,
        'uploads': uploads,
        })

@login_required
def edit_thing(request, slug):
    thing = Thing.objects.get(slug=slug)
    if thing.user != request.user:
        raise Http404

    form_class = ThingForm

    if request.method == 'POST':
        form = form_class(data=request.POST, instance=thing)
        if form.is_valid():
            form.save()
            return redirect('thing_detail', slug=thing.slug)
    else:
        form = form_class(instance=thing)

    return render(request, 'things/edit_thing.html',{'thing':thing,'form':form,})


def create_thing(request):
    form_class = ThingForm

    if request.method == 'POST':
        form = form_class(request.POST)
        if form.is_valid():
            thing = form.save(commit=False)
            thing.user = request.user
            thing.slug = slugify(thing.name)
            thing.save()

            return redirect('thing_detail', slug=thing.slug)

    else:
        form = form_class()

    return render(request, 'things/create_thing.html', {'form':form,})

def browse_by_name(request, initial=None):
    if initial:
        things = Thing.objects.filter(name__istartswith=initial)
        things = things.order_by('name')
    else:
        things = Thing.objects.all().order_by('name')

    return render(request, 'search/search.html', {'things':things,'initial':initial,})


def contact(request):
    form_class = ContactForm
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = form.cleaned_data['contact_name']
            contact_email = form.cleaned_data['contact_email']
            form_content = form.cleaned_data['content']

            template = get_template('contact_template.txt')

            context = Context({
                'contact_name':contact_name,
                'contact_email':contact_email,
                'form_content':form_content,
            })
            content = template.render(context)

            email = EmailMessage(
                'New contact form submission',content,
                'Your website <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f29a9bb2859796969b9c959e9d84979e8bdc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>>',['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a6c647e3e393d4a6d676b636624696567" rel="noreferrer noopener nofollow">[email protected]</a>'],
                headers = {'Reply-To':contact_email }
            )
            email.send()
            return redirect('contact')

    return render(request, 'contact.html', {'form': form_class, })

模型.py

from django.contrib.auth.models import User
from django.db import models

class Timestamp(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

#class Thing(Timestamp):
#    name = models.CharField(max_length=225)


#class Thing(models.Model):
class Thing(Timestamp):
    name = models.CharField(max_length=225)
    description = models.TextField()
    slug = models.SlugField()
    user = models.ForeignKey(User, blank=True, null=True, related_name="users")
    def get_absolute_url(self):
        return "/things/%s/" % self.slug

    def __unicode__(self):
        return self.name

class Social(models.Model):
    SOCIAL_TYPES = (
        ('twitter','Twitter'),
        ('facebook','Facebook'),
        ('pinterest','Pinterest'),
        ('instagram','Instagram'),
    )
    network = models.CharField(max_length=255, choices=SOCIAL_TYPES)
    username = models.CharField(max_length=255)
    thing = models.ForeignKey(Thing,related_name="social_accounts")

    class Meta:
        verbose_name_plural = "Social media links"

def get_image_path(instance, filename):
    return '/'.join(['thing_images', instance.thing.slug, filename])

class Upload(models.Model):
    thing = models.ForeignKey(Thing, related_name="upload")
    image = models.ImageField(upload_to=get_image_path)

上传图像并尝试查看后出现错误消息:

enter image description here

谁能帮我解决一下这个问题,谢谢!

最佳答案

Upload 模型有一个 thing 字段,其 lated_name 属性为 upload (不是 上传)。

关于python - “Thing”对象没有属性 'uploads',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764638/

相关文章:

小型迭代的 Python 双端队列性能

python - 如何在 python flask 服务器中处理 "413: Request Entity Too Large"

python flask 在每条路线上做一些事情

python - 如何从 django 项目中删除应用程序

python - Django Rest Framework ManyToMany 字段排序

Python:在不使用迭代的情况下将for循环操作扩展到矩阵中的每一行

python - Spark read.csv 错误地解析时间戳

python - 存储外键,不是来自表单 POST 方法,而是我已经拥有的表单数据

python - Django,在prefetch_related之后更新对象

Django Celery 超过了时间限制?