2016-03-03 77 views
2

嗨每一個我練習Django的書「你好網絡應用程序中間」,只是不知道爲什麼得到錯誤'東西'對象沒有屬性'上傳', ,,這是我的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 <[email protected]>',['[email protected]'], 
       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

任何人都可以幫我弄清楚問題,謝謝!

回答

3

Upload模型具有它有其related_name屬性作爲upload(未uploads)一個thing字段。

+1

謝謝你,在我更改uploads = thing.uploads.all()---> uploads = thing.upload.all(),,,,它可以工作 –

+0

那麼請將我的回答標記爲答案。謝謝。 – polarise

+1

我的聲望太低了,需要15點聲望才能評分答案,,,,還是非常感謝你 –

2

您可以查詢上傳模塊,獲取具有thing的所有Upload對象等於Thing對象。

uploads = Upload.objects.filter(thing=thing) 
+1

非常感謝你,它的作品! –