There is a few ways to route urls First Class Based then Function Based
Given an example:
Models.py
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class Item(models.Model):
name = models.CharField(max_length = 120)
description = models.TextField(blank = True, null = True)
price = models.DecimalField(decimal_places = 2, max_digits = 20)
def __unicode__(self):
return self.name
def get_absolute_url(self):
return reverse('item_detail', kwargs = {'pk' : self.pk})
# this function will return the url for item_detail with primary key
# url(r'^items/(?P<pk>\d+)/$', ItemDetailView.as_view(), name = 'item_detail'),
#here we are matching a url to pk and specify that id is a digit
# then set the class Based View
# then set the name of the url
# Primary key is automaticaly added by django, to every object
# it is same as id.
Views.py: Class Based
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
# Create your views here.
from .models import Item
class ItemListView(ListView):
model = Item
template_name = 'list_view.html'
def get_context_data(self, **kwargs):
context = super(ItemListView, self).get_context_data(**kwargs)
return context
class ItemDetailView(DetailView):
model = Item
template_name = 'detail_view.html'
def get_context_data(self, **kwargs):
context = super(ItemDetailView, self).get_context_data(**kwargs)
return context
urls.py
from items.views import *
urlpatterns = [
url(r'^items/$', ItemListView.as_view(), name = 'items'),
url(r'^items/(?P<pk>\d+)/$', ItemDetailView.as_view(), name = 'item_detail'),
]
list_view.html!!!!!!YOUR Answer is HERE!!!
{% extends "base.html" %}
{% block content %}
<br><br><br>
{% for item in item_list%}
<a href='{{item.get_absolute_url}}'><p>{{item.name}}</p></a>
<!-- these wil work exactly the same. How ever the first version is the most reliable. -->
<a href='{% url "item_detail" pk=item.pk %}'><p>{{item.name}}</p></a>
<!-- These are also Identical. How ever For class BASED VIEWS you MUST use
either Primary Key or a SLUG!!! -->
<a href='{% url "item_detail" pk=item.id %}'><p>{{item.name}}</p></a>
{% endfor%}
{% endblock content %}
detail_view.html
{% block content %}
<br><br><br>
<br><br><br>
<table class="table">
<tr>
<td>{{object.id}}</td>
<td>{{object.name}}</td>
<td>{{object.pk}}</td>
<td>{{object.description}}</td>
<td>{{object.price}}</td>
</tr>
</table>
{% endblock content %}
Views.py Function based
def item_detail_view_func(request, id):
item_instance = Item.objects.get(id =id)
template = 'detail_view.html'
context = {}
context['object'] = item_instance
return render(request, template, context)
Urls.py
url(r'^items/(?P<id>\d+)/$', item_detail_view_func, name = 'item_detail'),
list_view.html!!!!!!YOUR Answer is HERE!!!
{% for item in item_list%}
<!-- this version of urls will only work with function base views!!! -->
<a href='{% url "item_detail" id=item.id %}'><p>{{item.name}}</p></a>
{% endfor%}