In the Django project, in the posts application, I have a model:

class Post(models.Model): title = models.CharField(max_length=50) text = models.TextField() date = models.DateTimeField(auto_now_add=True) tags = models.ManyToManyField(Tag) def __str__(self): return self.title` 

The file views.py spelled:

 from django.shortcuts import render from django.views.generic.detail import DetailView from posts.models import Post class PostDetailView(DetailView): model = Post def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) return context 

post_detail.html:

 {% load static %} {% extends 'base.html' %} {% block title%} <h1>{{object.title}}</h1> {% endblock%} {% block content%} <p>{{object.text}}</p> <p>{{object.date}}</p> {% endblock%} 

and URLConf: url(r'^index/(?P<pk>\d+)/$', views.PostDetailView.as_view(), name='post_detail')

When I write in the address bar: localhost: 8000 / index / 1, then only the basic template is displayed, but there is no information about the post on the page, what could be the problem?

    1 answer 1

    Is the content block defined in the 'base.html' template?

    Try to replace

     {% block content%} <p>{{object.text}}</p> <p>{{object.date}}</p> {% endblock%} 

    on

     {% block content%} <p>test</p> <p>{{object.text}}</p> <p>{{object.date}}</p> {% endblock%} 

    Does the 'test' paragraph appear in the template?

    • Yes, the block is defined, but the paragraph 'test' did not appear, it didn’t work with the 'title' block either. Honestly, I didn’t quite understand how the DetailVew class defines a template that should be generated, I just didn’t find any mention in the official documentation that this should be indicated somewhere. - Igormalyga
    • The @igormalyga template name is determined by the model name and the class name, in your case the full path to the template: $TEMPLATE_DIR$/posts/post_detail.html . Try to determine the path to the template yourself by adding the line template_name = 'new_template.html' after model = Post , where new_template.html is the name of the template file. Then go to the URL and in the error message you will see in which directory it is trying to find this template. Move yours to that folder and rename it. - floydya
    • found an error after the line in URLConf was changed from url(r'^index/(?P<pk>\d+)/$', views.PostDetailView.as_view(), name='post_detail') to url(r'^(?P<pk>\d+)/$', views.PostDetailView.as_view(), name='post_detail') Everything worked, but I don’t understand why the link was url(r'^(?P<pk>\d+)/$', views.PostDetailView.as_view(), name='post_detail') in its previous state? - Igormalyga
    • @Igormalyga Most likely URLs are in order. The link is checked in order from top to bottom and as soon as the regular match coincides, it pulls out the view. You can try to rearrange your first version to the very top of the patterns and try again. - floydya pm