Reputation: 362
What is the best way to use slug in this case and make it work properly. I can see the URL on the browser display the items requested but i am unable to render the detailed page. I cant find where the issue is coming from. When i access 'page_detail' the url is 'http://127.0.0.1:8000/posts/2019/03/23/greetings/', which is correct based on my input but django throw an error to render the page. Error is: TypeError: post_detail() got an unexpected keyword argument 'slug'
MODEL:
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(User, on_delete = models.CASCADE, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
published = PublishedManager() # Custom Model Manager
def get_absolute_url(self):
''' Canonical URL for post detail.'''
return reverse('snippets:post-detail',
args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
URL
app_name = 'snippets'
urlpatterns = [
path('posts/', views.post_list, name='post-list'),
path('posts/<int:year>/<int:month>/<int:day>/<slug:slug>/', views.post_detail, name='post-detail'),
]
VIEWS
def post_list(request):
posts = Post.published.all()
context = {'posts': posts}
return render(request, 'snippets/list.html', context)
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'snippets/detail.html', {'post':post})
post_list HTML
{% extends "base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
post_detail HTML
{% extends "base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
I am still stuck. Any help would be much appreciated.
Upvotes: 1
Views: 170
Reputation: 261
try to change your views.py to
def post_detail(request, year, month, day, slug):
post = get_object_or_404(Post, slug=slug,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'snippets/detail.html', {'post':post})
Upvotes: 5