3๐
โ
You should have 2 views, one for the list and another for the details, so when you click on the item link in the list page, it will take you to the details view and template:
news/urls.py
from django.conf.urls import patterns, url, include
from django.views.generic import DetailView, ListView
from news import views
from news.models import News
urlpatterns = patterns('',
url(r'^$',
ListView.as_view(
queryset=News.objects.order_by('-post_date'),
context_object_name='allnews',
template_name='news/news.html'),
name='news_index'),
url(r'^(?P<id>\d+)/$',
DetailView.as_view(
model=News,
context_object_name='item',
template_name='news/news_item.html'),
name='news_detail'),
[...]
news/templates/news/news.html
[...]
{% for item in allnews %}
<h1 class="news"><a href="{% url 'news_detail' item.id %}">{{item.title}}</a></h1>
[...]
{% endfor %}
news/templates/news/news_item.html
<a href="{% url 'news_index' %}">Back</a>
<h1 class="news">{{item.title}}</h1>
<p>{{ item.body_text }}</p>
๐คalmalki
2๐
You are using a wrong view for details. You defined news_index
is a ListView
, you need to implement DetailView
. See more for Urls in generic views in django docs.
url(r'^(?P<id>\d+)/$',
DetailView.as_view(
model=News,
template_name='news/detail.html'),
name='news_detail'),
and then in template
{% for item in allnews %}
<h1 class="news"><a href="{% url 'news_detail' item.id %}">{{item.title}}</a></h1>
[...]
{% endfor %}
๐คAhsan
Source:stackexchange.com