I am Begging for any help i am circling around this for long time
The method get_absolute_url or even named pattern for details view never work at all
i got this error always while i have tried everything is possible and i am sure that there are no reason
# NoReverseMatch at /blog/posts/add/
Reverse for 'details' with keyword arguments '{'args': {'pk': UUID('066bf6a7-e171-42ef-bc5b-c056a3e15d6c')}}' not found. 1 pattern(s) tried: ['blog/posts/(?P<pk>[0-9]+)/\\Z']
it telling always that the Error caused by my create view function
here is my Post model
class Post(models.Model):
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True, editable=False) # add unique=True in slug (never repeat)
title = models.CharField(max_length=200, db_index=True, blank=False, default='')
snippet = models.OneToOneField(Snippet, on_delete=models.CASCADE, related_name='post_snippets', blank=True, null=True)
creator = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="post_creator")
...
def get_absolute_url(self):
"""Returns the url to access a particular post instance."""
return reverse('details', kwargs={ "pk": str(self.id)})
Here is my create View
def add_post(request):
if request.method == 'POST':
post_form = PostForm(request.POST, request.FILES)
snippet_form = SnippetForm(request.POST)
if post_form.is_valid() and snippet_form.is_valid():
post = post_form.save(commit=False)
snpt = snippet_form.save()
post.creator = request.user
post.snippet = snpt
post.save(),
snpt.save()
return redirect('blog:details', args={'pk': post.pk}) # or post.get_absolute_url()
else:
post_form = PostForm()
snippet_form = SnippetForm()
return render(request, 'blog/add_post.html', {'post': post_form, 'snpt': snippet_form})
it should go for details page after create new post
here is the simplest detail view
@login_required
def post_details(request, pk):
post = get_object_or_404(Post, id=pk)
return render(request, '/blog/post_details.html', {'post':post})
the url pattern is
path('posts/<int:pk>/', post_details, name='details'),
i have made all the changes to the get function and the pattern in view as args and kwargs but still got this error at different patterns
*searched a lot for any likely same as mine but i canāt find any applicable solution
Whatās wrong in my simple logic ?
how can i make the args work and be accepted?
I changed referring to index view after creating new post to refer to details, Also
I changed the get_absolute_url to {% url āblog:detailsā post.id %}" but still the same
the same problem happens when i try to get my all posts index page i got a problem with get_absolute_url method in my template index
Can the problem caused by the templates itself ???
any help please