我正在尝试使用图书管理员组获取具有作者ID的作者的详细信息。我也将其包含在settings.py文件中,并在postgresql db中设置了该组。购买一些不存在与组匹配的查询我正在运行服务器时显示。
目录结构:
untitled2
librarymanage
library
decorators
__init__.py
group_required.py
migrations
static
css
js
templatetags
__init__.py
has_group.py
xextends.py
__init__.py
admin.py
apps.py
forms.py
models.py
tables.py
tests.py
urls.py
validators.py
views.py
librarymanage
__init__.py
settings.py
urls.py
wsgi.py
templates
authors.html
authors_show.html
base.html
books.html
change_password.html
periods.html
periods_show.html
publisher_show.html
publishers.html
sign_in.html
sign_up.html
table_without_footer.html
db.sqlite3
manage.py
venv
views.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.hashers import check_password
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.models import User
from .forms import *
from .models import Book, LendPeriods, Author, Publisher, UserProfile
from .tables import BookTable, BookTableUser, AuthorTable,
PublisherTable,
PeriodsTable
from django_tables2 import RequestConfig
from django.contrib import messages
from django.utils import timezone
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .decorators.group_required import group_required
from django.db.models.base import ObjectDoesNotExist
from .templatetags import has_group
from django.http import HttpResponse
@login_required(login_url='/sign_in/')
def authors_show(request, author_id):
try:
author = Author.objects.get(id=author_id)
except ObjectDoesNotExist:
messages.error(request, "This author does not exist")
return redirect('/')
if author:
books_qs = Book.objects.filter(author=author_id)
books_table = BookTable(books_qs)
RequestConfig(request, paginate={"per_page": 5}).configure(books_table)
return render(request, 'authors_show.html',
{'author': author,
'books_table': books_table,
'books_qs': books_qs,
})
else:
messages.info(request, "Author does not exist")
return authors(request)
@login_required(login_url='/sign_in/')
def publishers_show(request, publisher_name):
try:
publisher = Publisher.objects.get(name=publisher_name)
except ObjectDoesNotExist:
messages.error(request, "This publisher does not exist")
return redirect('/')
if publisher:
books_qs = Book.objects.filter(publisher=publisher_name)
books_table = BookTable(books_qs)
RequestConfig(request, paginate={"per_page": 5}).configure(books_table)
return render(request, 'publisher_show.html',
{'publisher': publisher,
'books_table': books_table,
'books_qs': books_qs})
else:
messages.info(request, "Publisher does not exist")
return publishers(request)
authors_show.html
{% load django_tables2 %}
{% load xextends %}
{% load has_group %}
<!--"base.html" with active_tab=books small_panel=true center_panel_content=true-->
{% block content %}
<div class="page-title-outer"><div class="page-title">{{ author.name|title }} {{ author.surname|title }}</div></div>
<div class="book_desc">
Name: {{ author.name }}<br/>
Surname: {{ author.surname }}<br/>
Date of birth: {{ author.date_of_birth }}<br/>
</div>
{# List books by this author #}
{% if books_qs.count > 0 %}
{% autoescape off %}
{% render_table books_table "table_without_footer.html" %}
{% endautoescape %}
{% endif %}
<a href="{% url 'authors' %}">List of authors</a><br/><br/>
{% if request.user|has_group:"librarians" %}
<ul>
<li><a href="{% url 'edit' 'authors' author.id %}">Edit</a> this author</li>
<li><a href="{% url 'remove' 'authors' author.id %}"
onclick='return confirm("Do you really want to delete author: {{ author.name }} {{ author.surname }}?")'>Remove
</a> this author</li>
</ul>
{% else %}
{% endif %}
{% endblock %}
has_group.py
from django import template
from django.contrib.auth.models import Group
register = template.Library()
@register.filter(is_safe=True)
@register.filter(name='librarians')
def has_group(user, librarians):
"""
Checks if user belongs to certain group
"""
group = Group.objects.get(name=librarians)
return True if group in user.groups.all() else False
register.filter('librarians', has_group)
发生错误:
File "/home/divum/PycharmProjects/untitled2/librarymanage/library/templatetags/has_group.py", line 14, in has_group
group = Group.objects.get(name=librarians)
File "/home/divum/PycharmProjects/untitled2/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/divum/PycharmProjects/untitled2/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 380, in get
self.model._meta.object_name
DoesNotExist: Group matching query does not exist.
[04/Mar/2019 12:38:43] "GET /library/authors/show/3/ HTTP/1.1" 500 161171
Request Method: GET
Request URL: http://localhost:8000/library/periods/show/2/
Django Version: 1.11.20
Exception Type: TemplateSyntaxError
Exception Value:
Invalid filter: 'has_group'
Exception Location:
/home/divum/PycharmProjects/untitled2/venv/local/lib/python2.7/site-
packages/django/template/base.py in parse, line 515
Python Executable:
/home/divum/PycharmProjects/untitled2/venv/bin/python2.7
Python Version: 2.7.14