Answer the question
In order to leave comments, you need to log in
Why can't get the id of the django-mptt object?
Hello!
Created
[06/Mar/2019 22:12:14] "GET /udege/1 HTTP/1.1" 301 0
Internal Server Error: /udege/1/
Traceback (most recent call last):
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: category() got an unexpected keyword argument 'category_id'
[06/Mar/2019 22:12:14] "GET /udege/1/ HTTP/1.1" 500 70862
[06/Mar/2019 22:17:33] "GET /admin/about_udege/categories/ HTTP/1.1" 200 10660
[06/Mar/2019 22:17:33] "GET /admin/jsi18n/ HTTP/1.1" 200 14871
Internal Server Error: /udege/
Traceback (most recent call last):
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\YandexDisk\Проект\udege\about_udege\views.py", line 7, in index
return render(request, 'about_udege/index.html', context={'categories': categories})
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\defaulttags.py", line 309, in render
return nodelist.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 987, in render
output = self.filter_expression.resolve(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 671, in resolve
obj = self.var.resolve(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 796, in resolve
value = self._resolve_lookup(context)
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\template\base.py", line 858, in _resolve_lookup
current = current()
File "D:\YandexDisk\Проект\udege\about_udege\models.py", line 65, in get_absolute_url
return reverse('udege_categories_list', kwargs={'category_id': self.id})
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\urls\base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "D:\YandexDisk\Проект\udege\venv\lib\site-packages\django\urls\resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'udege_categories_list' with keyword arguments '{'category_id': 5}' not found. 1 pattern(s) tried: ['udege/$']
[06/Mar/2019 22:19:45] "GET /udege/ HTTP/1.1" 500 203862
from django.db import models
from django.shortcuts import reverse
from ckeditor.fields import RichTextField
from mptt.models import MPTTModel, TreeForeignKey
class Categories(MPTTModel):
class Meta:
verbose_name_plural = '1. Категории и подкатегории'
verbose_name = 'категорию или подкатегорию'
class MPTTMeta:
order_insertion_by = ['title']
parent = TreeForeignKey(
'self',
verbose_name='Родитель',
related_name='children',
on_delete='CASCADE',
default=0,
null=True,
blank=True,
)
title = models.CharField(
max_length=255,
verbose_name='Заголовок',
db_index=True,
blank=False
)
description = models.TextField(
verbose_name='Краткое описание',
default=None,
null=True,
blank=True,
)
body = RichTextField(
verbose_name='Основной текст',
db_index=True,
blank=True
)
created = models.DateTimeField(
verbose_name="Время создания",
auto_now_add=True
)
modified = models.DateTimeField(
verbose_name="Время изменения",
auto_now=True
)
@property
def count(self):
self_count = 0
children_count = 0
if hasattr(self, 'page_children'):
self_count = self.page_children.count()
if hasattr(self, 'children'):
children_count = len([children.count for children in self.children.all()])
return self_count + children_count
def get_absolute_url(self):
return reverse('udege_categories_list', kwargs={'category_id': self.id})
def __str__(self):
return self.title
from django.shortcuts import render, get_object_or_404
from .models import Categories
def index(request):
categories = Categories.objects.all
return render(request, 'about_udege/index.html', context={'categories': categories})
def category(request):
category = get_object_or_404(Categories, id__iexact=category_id)
return render(request, 'about_udege/category.html', context={'category': category})
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name='udege_categories_list'),
path('<str:category_id>/', category, name='udege_category_url'),
]
<div class="container col-container">
<div class="row row-flex row-flex-wrap">
{% for category in categories %}
{% if category.is_root_node %}
<div class="col-lg-3 col-md-4 col-sm-6 my-1">
<div class="card text-center">
<div class="card-body">
<h5 class="text-left">{{ category.title }}</h5>
<a href="{{ category.get_absolute_url }}">
<button type="button" class="btn btn-primary">Посмотреть</button>
</a>
</div>
</a>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
Answer the question
In order to leave comments, you need to log in
It says that there is no url like you are trying to build. udege_categories_list does not take any parameters - it leads to an index.
this one will not work and does not work, since it should not have kwargs.
return reverse('udege_categories_list', kwargs={'category_id': self.id})
It should probably be udege_category_url and not udege_categories_list
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question