Django NoReverseMatch :Reverse for 'blog_detail' with arguments '(1,)' not found - django

I created a topic as a categories for blog project.
I pointed 'topic' as a PK to my model
It is successfully to access by the follow path:
http://127.0.0.1:8000/django/1
and http://127.0.0.1:8000/IDE/2 django and IDE is the topic of my model
from django.shortcuts import render, get_object_or_404
from .models import Blog, Topic
from django.views.generic import DetailView
def post_list(request, topic_slug=None):
topic= None
topics = Topic.objects.all()
blogs = Blog.objects.filter(available=True)
if topic_slug:
topic = get_object_or_404(Topic, slug=topic_slug)
blogs = blogs.filter(topic=topic)
return render(request,
'topiclist.html',
{'topics': topics,
'topic':topic,
'blogs': blogs,
})
class BlogDetail(DetailView):
model = Blog
template_name ='detail.html'
from django.urls import path
from . import views
from .views import BlogDetail
urlpatterns = [
path('<slug:topic_slug>/', views.post_list, name='post_list'),
path('<slug:topic_slug>/<int:pk>/', BlogDetail.as_view(), name='blog_detail'),
]
from django.db import models
from ckeditor.fields import RichTextField
from django.urls import reverse
class Topic(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, default='self.name', db_index=True, unique=True)
class Meta:
verbose_name = 'topic'
verbose_name_plural = 'topics'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('post:topiclist', args=[self.slug])
class Blog(models.Model):
title = models.CharField(max_length=50)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
content = RichTextField()
topic = models.ForeignKey(Topic, default=1, on_delete=models.SET_DEFAULT)
available = models.BooleanField(default=True)
class Meta:
verbose_name = 'post'
verbose_name_plural = 'posts'
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post_detail', args=[str(self.id)])
It show the error
NoReverseMatch at /django/
Reverse for 'blog_detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['(?P<topic_slug>[-a-zA-Z0-9_]+)/(?P<pk>[0-9]+)/$']
when I use the the URL config of
blog_detail
My HTML template
{% block content %}
{% for blog in blogs %}
{{ topic.title }}</li>
{% endfor %}
{% endblock content %}
It works for using post_list but when I am using blog_detail it show the error. Any idea for this? I am new for django and programming. so many thanks if anyone could help. I read this question but it not help
Reverse for 'blog_detail' with no arguments not found. 1 pattern(s) tried: ['blog/(?P<pk>[0-9]+)$']

According to your urls.py code, the blog_detail url requires two arguments (the topic slug and the pk) and you only supplied the pk. If you pass in both, it should work (or refactor the url so the topic slug is not required).
{% url 'blog_detail' blog.topic.slug blog.pk %}

Related

Slugfield not working if field name is different from slug

Good evening, Django is completely new for me and it's my first question here.
I'm trying to create a Webapp. I'm using Django, Python and MariaDB.
I created a project with two apps and I have a model for each app. In the fist one I used "slug" as field name and everything is working fine. In the second one I wanted to differentiate that field giving a different name (bk_slug) defined as SlugField. I tried to use the same kind of instructions lines and modifying them for the field name it seems not working. I cannot have the right URL (Class based ListView) and cannot access to the Class based DetailView....
Thanks in advance for your support.
models.py
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.urls import reverse
# book masterdata
class Book(models.Model):
bk_title = models.CharField("Book Title", max_length=100,
primary_key=True)
bk_originaltitle = models.CharField("Book original Title",
max_length=100, blank=True)
bk_author = models.CharField("Author", max_length=50)
bk_editor = models.CharField("Editor", max_length=50)
bk_editiondate = models.CharField("Book Edition date",
max_length=30)
bk_recblocked = models.BooleanField("Book record blocked")
bk_creator = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
related_name='+', verbose_name="Created by")
bk_creationdate = models.DateTimeField("Creation date",
auto_now=True)
bk_modifier = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
related_name='+', verbose_name="Last modification by")
bk_modified = models.DateTimeField("Last modification date",
auto_now=True)
bk_slug = models.SlugField(max_length=100, allow_unicode=True,
blank=True)
def __str__(self):
return self.bk_title
def get_absolute_url(self):
return reverse('book_detail', kwargs={'slug': self.bk_slug})
urls.py
from django.urls import path
from dimlibrary.views import BookListView
from dimlibrary.views import BookDetailView
urlpatterns = [
path('', BookListView.as_view(), name='book_list'),
path('<slug:bk_slug>/', BookDetailView.as_view(),
name='book_detail'),
]
views.py
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
from django.utils import timezone
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from dimlibrary.models import Book
# book views :
# List view
class BookListView(ListView):
model = Book
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
# Details View
class BookDetailView(DetailView):
model = Book
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
```html
{% extends 'base/base.html' %}
{% load static %}
{% block content %}
<h1>Books List</h1>
<ul>
{% for book in object_list %}
<li><a href="{{ dimlibrary.book.get_absolute_url }}">{{
book.bk_title }}</a> - {{ book.bk_author }} -
{{ book.bk_editor }}</li>
{% empty %}
<li>No book registred yet.</li>
{% endfor %}
</ul>
{% endblock content %}
In your DetailView, you need to specify the slug_field [Django-doc] as 'bk_slug':
class BookDetailView(DetailView):
model = Book
slug_field = 'bk_slug'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
In the model, you will need to slugify(…) [Django-doc] the name, so:
from django.utils.text import slugify
class Book(models.Model):
# …
def save(self, *args, **kwargs):
if not self.bk_slug:
self.bk_slug = slugify(self.title)
return super().save(*args, **kwargs)

get_absolute_url: error of Reverse for 'article-detail' not found

I am using class based views to create a post. I have used get_absolute_url to go to the post page after clicking on post but it is giving an error of no reverse match.
this is my modelspy
from django.db import models
from django.conf import settings
from django.urls import reverse
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length = 50 , null=False,blank=False)
body = models.TextField(max_length = 5000 , null=False,blank=False)
date_published = models.DateTimeField(auto_now_add=True)
date_update = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('article-detail', args=(str(self.id)))
this is my urls.py:
urlpatterns = [
path('',views.home,name="home"),
path('home2/',HomeView.as_view(),name = "home2"),
path('article/<int:pk>',ArticleDetailView.as_view(),name = "article-detail"),
path('add_post/',AddPostView.as_view(),name="add_post"),
]
this is home2.html:
<ul>
{%for post in object_list %}
<li>{{post.title}}-{{post.author}}<br/>
{{post.body}}</li>
{%endfor%}
</ul>
this is views.py:
from django.shortcuts import render
from .models import *
from .forms import *
from django.views.generic import ListView,CreateView
from django.views.generic.detail import DetailView
# Create your views here.
def home(request):
return render(request,'post/home.html')
class HomeView(ListView):
model = BlogPost
template_name = "post/home2.html"
class ArticleDetailView(DetailView):
model = BlogPost
template_name = "post/article_details.html"
context_object_name = 'post'
class AddPostView(CreateView):
model = BlogPost
template_name = "post/add_post.html"
fields = '__all__'
I would suggest doing something like. I provide 2 methods.
1st method:
By using get_absolute_url
from django.urls import reverse
def get_absolute_url(self):
return reverse('post:article-detail', args=(self.id)) #post is the app_name
urls.py
url(r'^article/(?P<pk>\d+)/$', ArticleDetailView.as_view(), name='article-detail'),
home2.html
<ul>
{%for post in object_list %}
<li>
{{post.title}}-{{post.author}}<br/>{{post.body}}
</li>
{%endfor%}
</ul>
2nd Method:
By using template tag.Here get absolute url with help of template tag.
home2.html
{% load templatehelpers %}
<ul>
{%for post in object_list %}
<li>
{{post.title}}-{{post.author}}<br/>
{{post.body}}
</li>
{% endfor %}
</ul>
Here we use a new simple tag namely abs_url (absolute url) in templatehelpers.py (Template tag). And app_name is the name of the app.
templatehelpers.py
from django import template
from django.urls import reverse
register = template.Library()
#register.simple_tag
def abs_url(value, request, **kwargs):
return reverse(value,kwargs=kwargs)
If you change this
def get_absolute_url(self):
return reverse('article-detail', args=(str(self.id)))
to this
def get_absolute_url(self):
return reverse('home')
You will not get the error, but you will not either be redirected to the post you just posted.
So if your goal is to just get rid of this error, but don't care about where you get redirected afterwards, then this is the solution for you.

In the process of categorising my blogs but keep hitting the wall "Page not found 404"

I am a complete beginner so I apologies for my noobiness explanation. I am in the process of categorising my blogs.
I created a model -> migrated it -> imported in view.py -> Imported the view that I created into urls.py -> and created URL for my new page but when I add the href to other pages (home) it takes me to the error.
My codes:
models.py
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=100)
content=models.TextField()
date_posted=models.DateTimeField(default=timezone.now)
author=models.ForeignKey(User, on_delete=models.CASCADE)
category = models.CharField(max_length=100, default='Engineering')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
Views.py:
from .models import Post, Category
class CategoryListView(ListView):
model = Post
template_name= 'blog/category_posts.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 3
urls.py:
from django.urls import path
from .views import (
PostListView,
PostDetailView,
PostCreateView,
PostUpdateView,
PostDeleteView,
UserPostListView,
CategoryListView)
path('category/<str:category>/', CategoryListView.as_view(), name='category-posts'),
home.html
{{ post.category }}
In addition to what Arun said.
I think you need to set url_patters in your urls.py. Something like this:
from django.urls import path
from .views import (
PostListView,
PostDetailView,
PostCreateView,
PostUpdateView,
PostDeleteView,
UserPostListView,
CategoryListView)
urlpatterns = [
path('category/<str:category>/', CategoryListView.as_view(), name='category-posts'),
]
You should also pass the category in your url, like this
{{ post.category }}
Because your url needs a str parameter category.

No ReverseMatch URL django

I have a scenario that looks like this:
Models
from django.db import models
from django.core.urlresolvers import reverse_lazy, reverse
class State(models.Model):
short_name = models.CharField('Abbreviation', max_length=2)
state = models.SlugField('State Name', max_length=20)
def __str__(self):
return self.state
def get_absolute_url(self):
return reverse('state_list', kwargs={'state': self.state})
class City(models.Model):
state = models.ForeignKey(State)
city = models.CharField('City', max_length=100)
class Meta:
verbose_name = 'City'
verbose_name_plural = 'Cities'
def __str__(self):
return self.city
class School(models.Model):
name = models.CharField(max_length=69, default='')
def __str__(self):
return self.name
class Meta:
verbose_name = 'School'
verbose_name_plural = 'Schools'
Views
from django.shortcuts import get_object_or_404, render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from .models import School, City, State
def reviews_index(request):
state_list = State.objects.all()
context = {'states': state_list}
return render(request, 'reviews/reviews_index.html', context)
def state_detail(request, state=None):
city_list = City.objects.order_by('city')
context = {'cities': city_list}
return render(request, 'reviews/state_detail.html', context)
def city_detail(request, state=None, city=None):
school_list = School.objects.all()
context = {'schools': school_list}
return render(request, 'reviews/city_detail.html', context)
URLs
from django.conf.urls import url, include
from . import views
app_name = 'reviews'
urlpatterns = [
url(r'^$', views.reviews_index, name='reviews_index'),
url(r'^(?P<state>[a-z]+)/$', views.state_detail, name='state_detail'),
url(r'^(?P<state>[a-z]+)/(?P<city>[a-z]+)/$', views.city_detail, name='city_detail'),
]
However, when I try to create a link from the state_detail template to the city_detail template, I get this error:
NoReverseMatch at /reviews/alabama/
Reverse for 'city_detail' with arguments '('', 'Auburn')' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This is how I am linking in the template:
{% block main_content %}
<div class="content">
<div class="row">
{% if cities %}
{% for city in cities %}
<div class="medium-3 column">
{{ city.city }}
</div>
{% endfor %}
{% endif %}
</div>
</div>
{% endblock %}
Can someone please tell me that I am doing wrong and help me fix it. Thanks in advance.
The city_detail url pattern includes the state.state, so you need to include the state in the template context.
In your view, you can use get_object_or_404 to fetch the state using the slug.
def state_detail(request, state=None):
state = get_object_or_404(State, state=state)
city_list = City.objects.filter(state=state).order_by('city')
context = {'cities': `city_list`, 'state': state}
return render(request, 'reviews/state_detail.html', context)
Note that I've changed city_list, so that it only displays cities in the state you are viewing.
It's not a great idea to use the same variable name state for the instance and the slug. It would be a good idea to rename one of them, e.g. to state_obj or state_slug. If you do this, you'll have to make sure you update your urls, views and templates to be consistent.

Why is this Django class-based year archive view not working?

I'm trying to subclass the YearArchiveView class-based view to show a list of articles published in a year, but the filtering doesn't work and example.com/2012 shows articles from all years.
Note that I do not want the view code in urls.py. Rather, I want the LogbookYearArchive wrapper to continue living in views.py.
models.py:
class Entry(models.Model):
KIND = (
('L', 'Link'),
('A', 'Article'),
)
title = models.CharField(max_length=200)
slug = models.SlugField(unique_for_date='pub_date')
kind = models.CharField(max_length=1, choices=KIND, default=1,
help_text="Is this a link to other content or an original article?")
url = models.URLField(blank=True, help_text="The link URL")
body = models.TextField(blank=True)
body_html = models.TextField()
content_format = models.CharField(choices=CONTENT_FORMAT_CHOICES,
max_length=50, default=1)
is_active = models.BooleanField(help_text=_("Tick to make this entry\
live (see also the publication date). Note that administrators\
(like yourself) are allowed to preview inactive entries whereas\
the general public aren't."), default=True)
pub_date = models.DateTimeField(verbose_name=_("Publication date"),
help_text=_("For an entry to be published, it must be active and its\
publication date must be in the past."))
mod_date = models.DateTimeField(auto_now_add=True, editable=False)
class Meta:
db_table = 'blog_entries'
verbose_name_plural = 'entries'
ordering = ('-mod_date',)
get_latest_by = 'pub_date'
def __unicode__(self):
return self.title
#models.permalink
def get_absolute_url(self):
"""Construct the absolute URL for an Entry of kind == Article."""
return ('logbook-entry-detail', (), {
'year': self.pub_date.strftime("%Y"),
'month': self.pub_date.strftime("%m"),
'slug': self.slug})
urls.py:
from __future__ import absolute_import
from django.conf.urls import patterns, include, url
from .models import Entry
from .views import LogbookYearArchive
urlpatterns = patterns('',
url(r'^(?P<year>\d+)/$',
view=LogbookYearArchive.as_view(),
name='archive-year'),
)
views.py:
from django.views.generic.list import MultipleObjectMixin
from django.views.generic import ArchiveIndexView, MonthArchiveView, YearArchiveView, DetailView
from django.core.urlresolvers import reverse
from .models import Entry
class LogbookYearArchive(YearArchiveView):
"""Yearly archives of articles"""
model = Entry
date_field = 'pub_date'
year_format='%Y'
make_object_list=True,
template_name = 'hth/archive_year.html'
allow_future = False
def get_context_data(self, **kwargs):
context = super(LogbookYearArchive, self).get_context_data(**kwargs)
# =todo: fix filtering by date which is not working
context['object_list'] = Entry.objects.filter(
is_active=True, kind='A').order_by('-pub_date', 'title')[:9999]
return context
archive_year.html:
{% block content %}
<h1 style="margin-bottom:1em;">Articles Published in {{ year|date:"Y" }}</h1>
{% for object in object_list %}
{% ifchanged %}
<h2 class="dateline datelinearchive">{{ object.pub_date|date:"F Y" }}</h2>
{% endifchanged %}
<p>
{{ object.title }}
</p>
{% endfor %}
{% endblock %}
Can you try this:
class LogbookYearArchive(YearArchiveView):
"""Yearly archives of articles"""
model = Entry
date_field = 'pub_date'
year_format='%Y'
make_object_list=True,
template_name = 'hth/archive_year.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True, kind='A').order_by('-pub_date', 'title')
I added a queryset attribute and removed get_context_data()
I had the same problem. All ArchiveViews were working except YearArchiveView. Solution was found in make_object_list property. It should be True
That's a cut from django code
if not self.get_make_object_list():
# We need this to be a queryset since parent classes introspect it
# to find information about the model.
qs = qs.none()