A
A
Alla Zakharova2017-03-09 12:08:40
Django
Alla Zakharova, 2017-03-09 12:08:40

Why isn't the model view shown in django when clicking on a link?

There are two models - news and services. On the main page, they are displayed using templates without problems, but when you click on the link indicated on the page, for each model object, it goes to a new page, where only one model object should be displayed.
The news on the new page is displayed without problems. The page of the service is loaded, but the data is not displayed, everything is lost somewhere. How to be? The models are very similar.
Models:

class News(models.Model):
  title_news = models.CharField(max_length=100, verbose_name=_("Title"))
  image_news = models.ImageField(upload_to="media/", verbose_name=_("Image"))
  body_news = models.TextField(verbose_name=_("Text"))
  create_date = models.DateTimeField(default=timezone.now, verbose_name=_("Create date"))
  published_date = models.DateTimeField(blank=True, null=True, verbose_name=_("Published date"))

  def publish(self):
    self.published_date = timezone.now()
    self.save()

  def __str__(self):
    return self.title_news
  
  class Meta:
    verbose_name = _("News")
    verbose_name_plural = _("News")

class Service(models.Model):
  title_service = models.CharField(max_length=100, verbose_name=_("Title of service"))
  image_service = models.ImageField(upload_to="media/", verbose_name=_("Image"))
  text_of_service = models.TextField(verbose_name=_("Text"))

  def __str__(self):
    return self.title_service

In wiews.py, the method for displaying by reference is set like this:
def post_detail(request, pk):
  post = get_object_or_404(News, pk=pk)
  return render(request, 'blog/post_detail.html', {'post': post})

def service_detail(request, pk):
  block = get_object_or_404(Service, pk=pk)
  return render('blog/service_detail.html', {'block': block})

In urls.py it is written like this:
urlpatterns = [
  url(r'^$', views.index, name='index'),
  url(r'^get_more_news/$', views.get_more_news, name='more_news'),
  url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
  url(r'^service/(?P<pk>[0-9]+)/$', views.service_detail, name='service_detail'),
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alla Zakharova, 2017-03-10
@Mi5aka

Salavat Sharapov
Work news template:

{% for post in news %}
          <div class="cell-md-4">
            <div class="blog-post-default">
              <a href="blog-single-post.html" class="reveal-inline-block thumbnail-custom">
                <img src="/media/{{ post.image_news }}" width="370" height="250" alt="" class="img-responsive">
              </a>
              <div class="blog-post-default-title">
                <a href="{% url 'post_detail' pk=post.pk %}">{{ post.title_news }}</a>
              </div>
              <p class="blog-post-default-descr">{{ post.body_news | truncatewords:30 }} <a href="{% url 'post_detail' pk=post.pk %}">Читать далее</a></p>
              <div class="text-gray blog-post-default-meta">by <a href="{% url 'post_detail' pk=post.pk %}">Автор записи</a><span class="blog-post-default-date">{{ post.published_date }}</span>
              </div>
            </div>
          </div>
        {% endfor %}

Template displayed for a separate news link (works):
<section>
    <div class="shell">
      <ol class="breadcrumb">
        <li><a href="./">Назад</a></li>
        <li class="active">{{ post.title_news }}</li>
      </ol>
    </div>
  </section>

  <section class="section-bottom-56 offset-top-8">
    <div class="shell">
      <div class="range text-md-left">
        <div class="cell-md-8 blog-single-post">
          <div class="qweqwe">
            <h3>{{ post.title_news }}</h3>
              <div class="blog-post-meta offset-top-8"><span class="blog-post-author">by <a href="blog-list.html">Автор записи</a></span><span><a href="blog-list.html">{{ post.published_date }}</a></span>
                </div><img src="/media/{{ post.image_news }}" alt="" class="img-responsive blog-post-media offset-top-24">

                <p>{{ post.body_news|linebreaksbr }}</p>
              </div>
            </div>
          </div>	
        </div>		
      </section>

Service template (working):
{% for block in services|slice:":3"  %}
                <div class="cell-md-for-service">
                        <a href="blog-single-post.html" class="reveal-inline-block thumbnail-custom">
                            <img src="/media/{{ block.image_service }}" class="circle">
                        </a>
                        <div class="title_service_1">
                            <a href="{% url 'service_detail' id=block.id %}">{{ block.title_service }}</a>
                        </div>
                </div>
            {% endfor %}

Template displayed for a separate service on the link ( does not work ):
<section>
    <div class="shell">
      <ol class="breadcrumb">
        <li><a href="./">Назад</a></li>
        <li class="active">{{ block.title_service }}</li>
      </ol>
    </div>
       </section>

  <section class="section-bottom-56 offset-top-8">
    <div class="shell">
      <div class="range text-md-left">
        <div class="cell-md-8 blog-single-post">
          <div class="qweqwe">
            <h3>{{ block.title_service }}</h3>
              <div class="blog-post-meta offset-top-8"><span class="blog-post-author">by <a href="blog-list.html">Автор записи</a></span><span><a href="blog-list.html">...</a></span>
                </div><img src="/media/{{ block.image_service }}" alt="" class="img-responsive blog-post-media offset-top-24">

                <p>{{ block.text_of_service|linebreaksbr }}</p>
              </div>
            </div>
          </div>	
        </div>		
  </section>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question