2013-03-16 97 views
8

我的應用程序允許你喜歡的圖片,然後它會將您重定向到同一頁面Django的「功能」對象有沒有屬性「對象」

我得到那麼錯誤,當我嘗試喜歡的圖片,我可以創造一個像對象與外殼提示符,但爲什麼我得到這個錯誤?感謝對我的幫助

AttributeError at /like/3/ 
function' object has no attribute 'objects'Request Method: GET 
Request URL: http://127.0.0.1:8000/like/3/ 

Exception Value: 'function' object has no attribute 'objects' 
Traceback: 
File "C:\Python26\Lib\site-packages\django\core\handlers\base.py" in get_response 
111.       response = callback(request, *callback_args, **callback_kwargs) 
File "C:\o\mysite\pet\views.py" in Like 
195.  new_like, created = Like.objects.get_or_create(user=request.user, picture_id=picture_id) 

這是我的views.py的部分

def Like(request,picture_id): 
    pid = picture_id 
    new_like, created = Like.objects.get_or_create(user=request.user, picture_id=picture_id) 
    p = Picture.objects.get(pk=pid) 
    if created: 
     HttpResponseRedirect(reverse('world:url_name')) 
    else: 
     HttpResponseRedirect(reverse('world:url_name')) 

我的URL配置:

 url(

我的模型的一部分: R'^象/(P \ d +)/ $', 'pet.views.Like', name ='Like' ), My boat.html

{% if picture %} 
<ul> 
    {% for pet in picture %} 
    <li><b>description</b> = {{ pet.description}}<br/> 
     {% if pet.image %} 
<li> 
    <a href ="{% url world:Like pet.id %}"> 
     <img src= "{{ pet.image.url }}" style="cursor:pointer"> 
    </a> 
<li> 
     {% endif %} 
{% endfor %} 
</ul> 
{% endif %} 
<a href="{% url world:PictureCreator %}">Add Pictures to your board</a><br/> 

我的models.py

class Picture(models.Model): 
    user = models.ForeignKey(User) 
    board = models.ForeignKey(Board,blank=False,null=False) 
    image = models.FileField(upload_to="images/",blank=True) 
    description = models.TextField() 
    is_primary = models.BooleanField(default=False) 
    def __unicode__(self): 
     return self.description 

class Like(models.Model): 
    user = models.ForeignKey(User) 
    picture = models.ForeignKey(Picture) 
    created = models.DateTimeField(auto_now_add=True)  

回答

8

您的視圖功能名稱定義爲Like和模型名爲Like

定義Like的功能,所以當你去訪問Like.objects Python做看不到你的模型Like但功能Like

你湊ld重命名您的視圖功能

url(r'^like/(?P\d+)/$', 'pet.views.change_name_no_conflict', name = 'Like') 


def change_name_no_conflict(request,picture_id): 
    pass 
+1

感謝ü幫助我 – donkeyboy72 2013-03-16 14:07:04

1

型號名稱和視圖名稱不應該相同。

0

我做

from django.contrib.auth import get_user_model 
User = get_user_model 

增加()解決了這個錯誤時,得到了同樣的錯誤:

from django.contrib.auth import get_user_model 
User = get_user_model() 
相關問題