'Thing'对象没有属性'uploads'

时间:2016-03-03 06:22:14

标签: python django

嗨,每一个我都是通过一本书“hello web app intermediate”练习Django,只是不知道为什么得到错误'Thing'对象没有属性'uploads',,,这里是我的views.py:< / p>

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 <hi@weddinglovely.com>',['fnt437@gmail.com'],
                headers = {'Reply-To':contact_email }
            )
            email.send()
            return redirect('contact')

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

models.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

任何人都可以帮我解决问题,谢谢!

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

您可以查询上传MOdel以获取Upload对象thing等于Thing对象的所有uploads = Upload.objects.filter(thing=thing) 个对象。

eval "$(docker-machine env default)"