Python Django';功能';对象没有属性';对象';
我的应用程序允许你喜欢一张图片,然后它会将你重定向到同一页面 当我尝试喜欢一张图片时,我会得到一个错误,我可以用shell提示符创建一个类似的对象,但是为什么会出现这个错误呢?谢谢你帮助我Python Django';功能';对象没有属性';对象';,python,django,Python,Django,我的应用程序允许你喜欢一张图片,然后它会将你重定向到同一页面 当我尝试喜欢一张图片时,我会得到一个错误,我可以用shell提示符创建一个类似的对象,但是为什么会出现这个错误呢?谢谢你帮助我 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: 'f
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)
这是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'))
我的URLconf:
url(
我的模型的部分:
r'^like/(?P\d+/$),
“宠物视图,比如”,
name='Like'
),
我的船.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/>
视图函数名称定义为
Like
,模型名称为Like
您将Like
定义为一个函数,因此当您访问Like.objects
python时,不会看到您的模型Like
,而是函数Like
您可以重命名视图函数
url(r'^like/(?P\d+)/$', 'pet.views.change_name_no_conflict', name = 'Like' )
def change_name_no_conflict(request,picture_id):
pass
模型名和视图名不应该相同。我在执行此操作时遇到了相同的错误
from django.contrib.auth import get_user_model
User = get_user_model
添加()解决了错误:
from django.contrib.auth import get_user_model
User = get_user_model()
函数名和模型名确实取决于名称,函数名应该与我们在url中定义的url名相同。py模型名取决于函数数据成员,其含义例如,当我们从用户获取数据并保存在数据库中时,我们从其模型名ex=u_data=registration()调用该对象,这用于用户数据服务,并定义了我们发送数据以保存的乐趣,即在目标url及其相关视图中 定义对象:
objects = models.Manager()
你的错误会解决的 这是因为view.py中的函数名和models.py中的模型名是相同的。 您可以更改函数名或模型名 另一个解决办法是:
from .models import modelname as modelname2
def modelname(request):
obj_list_ads = modelname2.objects.all()
在此代码中,函数名为modelname。非常有用!谢谢
from .models import modelname as modelname2
def modelname(request):
obj_list_ads = modelname2.objects.all()