Pal*_*pta -1 html python sqlite django
I am developing a website which is based on information about books and i want to display all the authors present in the 'Authors' table in an HTML page. When i click on the link "authors", the page is not rendered and it gives an error "User matching query does not exist." (Not letting me post the image here). This is the traceback from the terminal.
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.
Run Code Online (Sandbox Code Playgroud)
I tried running the query in python manage.py shell and it yields a proper query set.
My urls.py file is:
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'),
]
Run Code Online (Sandbox Code Playgroud)
My View function is:
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')
Run Code Online (Sandbox Code Playgroud)
My Html page authors.html is:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
The view function "follow_user" is just for an entry into the database, it does not have an HTML page associated with it.
test.html is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% load friendshiptags %}
<h1> {% following request.user %} </h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
I have visited different links to the similar questions but none of them solves my problem. link1: objects.all() query not working link2: Django objects.all() empty query set, not empty in shell
The help is greatly appreciated. Thanks
Your request to /books/authors/ is being handled by the follow_user view, because they both match, and the follow_user pattern is above the authors pattern.
# 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'),
Run Code Online (Sandbox Code Playgroud)
You could fix this by changing the regexes so that they don't clash, or by moving the authors pattern above the follow_user pattern (note this will stop you from following a user with username='authors').
Once you've fixed your URL patterns, the traceback suggests that there is a problem in your follow_user view that you should fix:
user_to_be_followed = User.objects.get(username=username)
Run Code Online (Sandbox Code Playgroud)
If there is a chance that the user does not exist in the database, then you'll want to handle this possibility as well. You could catch the exception:
try:
user_to_be_followed = User.objects.get(username=username)
except User.DoesNotExist:
user_to_be_followed = None
Run Code Online (Sandbox Code Playgroud)
或者你可以使用get_object_or_404快捷方式:
from django.shortcuts import get_object_or_404
user_to_be_followed = get_object_or_404(User, username=username)
Run Code Online (Sandbox Code Playgroud)
最后,请注意以下检查是不正确的:
user_following = request.user
if user_following is not None:
...
Run Code Online (Sandbox Code Playgroud)
如果用户未登录,request.user则将是匿名用户,而不会是None。您应该检查if request.user.is_authenticated:Django 1.10+ 或if request.user.is_authenticated():更早版本。
| 归档时间: |
|
| 查看次数: |
4951 次 |
| 最近记录: |