我正在开发一个基于关于书籍的信息的网站,并且我想要显示出现在HTML页面的作者表中的所有作者。当我点击链接“作者”时,页面没有呈现,并且出现错误“用户匹配查询不存在”。 (不让我在这里发布图片)。 这是从终端回溯。.objects.all()查询在Django中不起作用
Internal Server Error: /books/authors/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Pallav\PycharmProjects\onlineBookStore\onlineBookStore\books\views.py", line 35, in follow_user
user_to_be_followed = User.objects.get(username=username)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 380, in get
self.model._meta.object_name
DoesNotExist: User matching query does not exist.
我试着运行python manage.py shell
中的查询,它会产生一个正确的查询集。
我urls.py
文件是:
app_name = 'books'
urlpatterns = [
# /books/
url(r'^$', views.IndexView.as_view(), name='index'),
# /register/
url(r'register/$', views.UserFormView.as_view(), name='register'),
# /logout/
url(r'logout/$', views.logout_user, name='logout'),
# /login/
url(r'login/$', views.login_user, name='login'),
# /search/
url(r'search/$', views.search, name='search'),
# /add_book/
url(r'search/(?P<isbn>[0-9]+)/add_book$', views.add_book, name='add_book'),
# /books/<book_id>/
url(r'^(?P<book_id>[0-9]+)/$', views.detail, name='detail'),
# /books/favorite/
url(r'^(?P<book_id>[0-9]+)/favorite/$', views.favorite, name='favorite'),
# /books/rate/
url(r'^(?P<book_id>[0-9]+)/rate/$', views.rate, name='rate'),
# /books/review/
url(r'^(?P<book_id>[0-9]+)/review/$', views.review, name='review'),
# /books/borrow/
url(r'^(?P<book_id>[0-9]+)/borrow/$', views.borrow, name='borrow'),
# books/user_profile/
url(r'user_profile/$', views.user_profile, name='user_profile'),
# books/edit_profile
url(r'edit_profile/$', views.edit_profile, name='edit_profile'),
# books/login_btn
url(r'login_btn/$', views.login_btn, name='login_btn'),
# books/show_users
url(r'show_users/$', views.show_users, name='show_users'),
# books/<username>/
url(r'^(?P<username>[a-z]*[A-Z]*[0-9]*)/$', views.follow_user, name='follow_user'),
# books/authors/
url(r'authors/$', views.authors, name='authors'),
]
我的视图功能是:
def authors(request):
all_authors = Authors.objects.all()
return render(request, 'books/authors.html', {'all_authors': all_authors})
def follow_user(request, username):
user_following = request.user
if user_following is not None:
user_to_be_followed = User.objects.get(username=username)
Follow.objects.add_follower(user_following, user_to_be_followed)
return render(request, 'books/test.html')
else:
return render(request, 'books/registration_form.html')
我的HTML页面authors.html是:
{% extends 'books/base.html' %}
{% block title %}All authors{% endblock %}
{% block body %}
<div class="users-container container-fluid">
<!-- Books -->
<div class="row">
{% if all_authors %}
{% for author in all_authors %}
<div class="col-sm-4 col-lg-2">
<div class="thumbnail">
<h1>{{ author.name }}</h1>
</div>
</div>
<br>
{% endfor %}
{% else %}
<h3>Currently no authors available</h3>
{% endif %}
</div>
</div>
{% endblock %}
视图功能 “follow_user” 是只是为了进入数据库,它没有与其关联的HTML页面。
test.html的是
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% load friendshiptags %}
<h1> {% following request.user %} </h1>
</body>
</html>
我曾参观过不同的链接到类似的问题,但他们没有解决我的问题。链接1:objects.all() query not working 链接2:Django objects.all() empty query set, not empty in shell
非常感谢您的帮助。由于
请再次查看您的堆栈跟踪。您提供的视图不是导致问题的视图。在“File”C:\ Users \ Pallav \ PycharmProjects \ onlineBookStore \ onlineBookStore \ books \ views.py中调用'user_to_be_followed = User.objects.get(username = user_id)'时,出现问题,第35行,follow_user' – an0o0nym
如你所见,你没有在功能上进行任何查询。当然你的问题在urls.py中。然后发布完整的urls.py代码 – Aniket
,@ alasdair的编辑答案应该适合你。 – Aniket