Answer the question
In order to leave comments, you need to log in
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
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})
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
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 %}
<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>
{% 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 %}
<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 questionAsk a Question
731 491 924 answers to any question