Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python .objects.all()查询在Django中不起作用_Python_Html_Django_Sqlite - Fatal编程技术网

Python .objects.all()查询在Django中不起作用

Python .objects.all()查询在Django中不起作用,python,html,django,sqlite,Python,Html,Django,Sqlite,我正在开发一个基于书籍信息的网站,我想在HTML页面中显示“authors”表中的所有作者。当我点击“authors”链接时,页面不会被呈现,它会给出一个错误“用户匹配查询不存在”(不允许我在这里发布图像)。 这是终端的回溯 Internal Server Error: /books/authors/ Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\core\handlers\exce

我正在开发一个基于书籍信息的网站,我想在HTML页面中显示“authors”表中的所有作者。当我点击“authors”链接时,页面不会被呈现,它会给出一个错误“用户匹配查询不存在”(不允许我在这里发布图像)。 这是终端的回溯

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
中运行该查询,结果得到了一个合适的查询集

我的
url.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'),
]
我的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 %}
{%extends'books/base.html%}
{%block title%}所有作者{%endblock%}
{%block body%}
{%if所有_作者%}
{所有作者中的作者%}
{{author.name}

{%endfor%} {%else%} 目前没有可用的作者 {%endif%} {%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>

标题
{%load friendshiptags%}
{%following request.user%}
我访问了类似问题的不同链接,但没有一个能解决我的问题。链接1: 链接2:


非常感谢你的帮助。谢谢

您向我们展示的代码似乎很好,但在您的代码中,有这样一行代码:

User.objects.get(username=user_id)
由于您试图将id与用户名匹配,因此它将找不到任何内容,而当
get()
未找到任何内容时,它将抛出异常


你应该做一些事情来捕捉错误,比如
除了User.DoesNotExists
并修复你的情况。

你对
/books/authors/
的请求正在由
follow\u User
视图处理,因为它们都匹配,
follow\u User
模式在
authors
模式之上

# 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'),
如果数据库中可能不存在该用户,那么您也需要处理这种可能性。您可以捕获异常:

try:
    user_to_be_followed = User.objects.get(username=username)
except User.DoesNotExist:
    user_to_be_followed = None
或者您可以使用
get\u object\u或\u 404
快捷方式:

from django.shortcuts import get_object_or_404

user_to_be_followed = get_object_or_404(User, username=username)
最后,请注意以下检查不正确:

user_following = request.user 
if user_following is not None:
    ...

如果用户未登录,则
request.user
将是匿名用户,而不是
None
。您应该在Django 1.10+中检查
if request.user.is_authenticated:
,或者在早期版本中检查
if request.user.is_authenticated():

请再次查看堆栈跟踪。您提供的视图不是导致问题的视图。在follow\u user`第35行的`File“C:\Users\Pallav\PycharmProjects\onlineBookStore\onlineBookStore\books\views.py”中调用
user\to\u follower=user.objects.get(username=user\u id)
时会出现问题。当然,您的问题在于url.py。发布完整的URL.py代码好吧,@alasdair编辑的答案应该适合您。另外,当您跟踪/取消跟踪用户时,您应该使用Post请求而不是get请求,因为您正在更改数据。进行更改后,通常的做法是重定向而不是渲染视图。注意!再次感谢:)作者模块不需要任何用户,也不需要对用户表进行查询。此外,authors表与user表没有任何关系(读:foreign\u key等)。我不知道为什么回溯显示了这句话。我将编辑我的问题,将这些细节也包括在内。而且,我在命名上犯了一个错误。user_id实际上是用户名。谢谢,我会解决的。回溯会告诉你错误发生的地方。如果代码不在
authors
视图中,则表示Django没有运行authors视图。您的url模式可能有错误(例如,缺少
$
的正则表达式)-您尚未显示完整的url,因此我无法确定。如果您更改了代码,则应添加新的回溯-回溯中不应再包含
User.objects.get(username=User\u id)
。非常感谢您的回复!成功了。对于这个问题的无能框架,我感到抱歉。我是新来的。还在学习:)很高兴它成功了。随着练习的增加,阅读回溯变得越来越容易,您将能够更快地找出问题:)
from django.shortcuts import get_object_or_404

user_to_be_followed = get_object_or_404(User, username=username)
user_following = request.user 
if user_following is not None:
    ...