Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 'function' object has no attribute 'objects'

Tags:

python

django

My apps allow you to like a picture then it redirects you to the same page

I get then error when I try to like a picture I can create a like objects with the shell prompt but why I get this error? thank for helping me

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)

This is parts of my 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'))

My URLconf:

     url(

Parts of my model: r'^like/(?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/>

My 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)    
like image 644
donkeyboy72 Avatar asked Mar 16 '13 13:03

donkeyboy72


1 Answers

your view function name is defined as Like and your model is named Like

you define Like as a function so when you go to access Like.objects python does not see your model Like but the function Like

you could rename your view function

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


def change_name_no_conflict(request,picture_id):
  pass
like image 200
dm03514 Avatar answered Oct 05 '22 18:10

dm03514