Good evening, I write my first project on django and there was a problem, (I am writing a blog) when I add a comment to the article, it translates to the wrong page and writes that there is no page, (Page not found (404)) I will be very grateful for your help = ) Projects threw on github here: https://github.com/Dudoser/blog.git

  • You should add a .gitignore file to the project and exclude unnecessary files from the repository, for example, it does not make sense to store a virtual environment in it (it’s common practice to add the requirements.txt file instead of listing the necessary dependencies, you can create it with the pip freeze > requirements.txt command, and after that, anyone can easily recreate the environment locally with the command pip install -r requirements.txt ), the sqlite3 database is also redundant and the IDE settings (the .idea folder). In fact, there should be only a folder firstapp - the most pulp =) - Pavel Karateev

1 answer 1

You have an error in urls.py :

 url(r'^articles/addcomment/(?P<article_id>\d+)/$', 'article.views.addcomment'), 

article should be article instead - you use /article/addcomment/ .

The same applies to:

 url(r'^articles/get/(?P<article_id>\d+)/$', 'article.views.article'), 

In the view addcomment you refer to /article/get/%s/ .

And another point regarding the view article , it seems to me that you can throw half of the code out of it without harm to the right thing:

 def article (request, article_id): article = Article.objects.get(id=article_id) comments = Comments.objects.filter(comments_article_id=article_id) form = CommentForm() return render(request, 'article.html', { 'article': article, 'comments': comments, 'form': form }) 

I would advise to get acquainted with the leadership of DjangoGirls . They also write a blog there and explain pretty well how to do everything neatly.

  • Yes, indeed, I missed the 's' in the view and wrote the extra 's' in the legal page, but can you tell me why comments are not displayed on the site ?? (they are added to the database) writing in forms.py fields = ['comments_text'] did not help - Phasm
  • @Phasm comments are not displayed because in article.html you are article.html over the comments variable: {% for comment in comments %} , and from view you pass 'comment', I tested with the implementation of article from the answer and everything was displayed correctly. - Pavel Karateev