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
1 answer
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 inarticle.htmlyou arearticle.htmlover thecommentsvariable:{% for comment in comments %}, and from view you pass 'comment', I tested with the implementation ofarticlefrom the answer and everything was displayed correctly. - Pavel Karateev
|
.gitignorefile 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 therequirements.txtfile instead of listing the necessary dependencies, you can create it with thepip freeze > requirements.txtcommand, and after that, anyone can easily recreate the environment locally with the commandpip install -r requirements.txt), the sqlite3 database is also redundant and the IDE settings (the.ideafolder). In fact, there should be only a folderfirstapp- the most pulp =) - Pavel Karateev