Код: Выделить всё
NoReverseMatch at /comment/create/the-art-of-self-care-prioritizing-your-well-being/
Reverse for 'comment-create' with arguments '('',)' not found. 1 pattern(s) tried: ['comment/create/(?P[-a-zA-Z0-9_]+)/\\Z']
Request Method: POST
Request URL: http://localhost:8000/comment/create/the-art-of-self-care-prioritizing-your-well-being/
Django Version: 5.0.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'comment-create' with arguments '('',)' not found. 1 pattern(s) tried: ['comment/create/(?P[-a-zA-Z0-9_]+)/\\Z']
Exception Location: C:\Users\shafa\PycharmProjects\CampusConnect\.venv\Lib\site-packages\django\urls\resolvers.py, line 848, in _reverse_with_prefix
Raised during: comments.views.CommentCreateView
Python Executable: C:\Users\shafa\PycharmProjects\CampusConnect\.venv\Scripts\python.exe
Python Version: 3.12.1
Python Path:
['C:\\Users\\shafa\\PycharmProjects\\CampusConnect',
'C:\\Users\\shafa\\PycharmProjects\\CampusConnect',
'C:\\Program Files\\JetBrains\\PyCharm '
'2023.2.5\\plugins\\python\\helpers\\pycharm_display',
'C:\\Python312\\python312.zip',
'C:\\Python312\\DLLs',
'C:\\Python312\\Lib',
'C:\\Python312',
'C:\\Users\\shafa\\PycharmProjects\\CampusConnect\\.venv',
'C:\\Users\\shafa\\PycharmProjects\\CampusConnect\\.venv\\Lib\\site-packages',
'C:\\Program Files\\JetBrains\\PyCharm '
'2023.2.5\\plugins\\python\\helpers\\pycharm_matplotlib_backend']
Server time: Mon, 16 Sep 2024 12:56:05 +0000
Код: Выделить всё
models.py
Код: Выделить всё
class Comment(models.Model):
content = models.TextField()
author = models.ForeignKey('users.User', related_name='comments', on_delete=models.CASCADE)
post = models.ForeignKey('posts.Post', related_name='comments', on_delete=models.CASCADE)
date_posted = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.author.username + " -> " + self.post.title
Код: Выделить всё
views.py
Код: Выделить всё
class CommentCreateView(LoginRequiredMixin, CreateView):
model = Comment
fields = ['content']
def form_valid(self, form):
form.instance.author = self.request.user
form.instance.post = Post.objects.get(slug=self.kwargs['slug'])
return super().form_valid(form)
def get_success_url(self):
return reverse('post-detail', kwargs={'slug': self.object.post.slug})
Код: Выделить всё
urls.py
Код: Выделить всё
urlpatterns = [
path('create//', views.CommentCreateView.as_view(), name='comment-create'),
]
Код: Выделить всё
comment_form.html
Код: Выделить всё
{% csrf_token %}
Comment
Submit
{{ post.slug }}
{{ post.title }}
URL-адрес, который я можно увидеть в браузере: http://localhost:8000/comment/create/th ... ell-being/, где-art-of-self-care -приоритизация вашего благополучия — это пул.
Я пробовал разные способы передачи параметра пула и все перепроверял. Но ничего не помогло.
Подробнее здесь: https://stackoverflow.com/questions/789 ... -in-django