Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

python - ModuleNotFoundError at /blog/post/comment/update/18/ No module named 'test-post-ecomon'

I have a blog section of my website and I made an CommentUpdateView and after submiting the form it gives me this error

ModuleNotFoundError at /blog/post/comment/update/18/
No module named 'test-post-ecomon'

but I don't undersant why because this post exists and I implemented get_absolute_url in my Contact models and It should work right? When I create a new commment it works.

view

class CommentUpdateView(LoginRequiredMixin, UpdateView):
    model = Comment
    form_class = CommentUpdateForm
    template_name = 'blog/update_comment.html'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

    def get_queryset(self, *args, **kwargs):
        return super().get_queryset(*args, **kwargs).filter(author=self.request.user)

model

class Comment(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    reply = models.ForeignKey('self', on_delete=models.CASCADE, null=True, related_name='replies')
    content = models.TextField(_('Content'))
    posted_date = models.DateTimeField(_('Posted Date/Time'), auto_now_add=True)
    updated_date = models.DateTimeField(_('Updated Date/Time'), auto_now=True)

    def __str__(self):
        return f'{self.author.username} - {self.post.title}'

    def get_absolute_url(self):
        return reverse('post', self.post.slug)

urls

path('post/<slug:slug>/', PostDetailView.as_view(), name='post'),
path('post/comment/update/<int:pk>/', CommentUpdateView.as_view(), name='update-comment'),
question from:https://stackoverflow.com/questions/65861351/modulenotfounderror-at-blog-post-comment-update-18-no-module-named-test-post

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try rewriting your get_absolute_url like this:

def get_absolute_url(request):
    return reverse('post', args=(self.post.slug, ))

P.S. You dont really want to reassign user in your update view since it decreases performance whilte doing basically nothing


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...