Monday, July 25, 2016

Reverse URL django-admin

I apologise in advance for the length of my post but I have included all the detail I can.

I have the following structure for an app. The list view template uses Bootstrap DataTables. My question is how to amend the urls to return the change url in django-admin.


models.py

def get_absolute_url(self):
        return reverse('contact_detail', kwargs={"id": self.id})

urls.py


from django.conf.urls import url
from .views import (
    contacts_list,
    contacts_detail,
)


urlpatterns = [
    url(r'^$', contacts_list, name='contact_list'),
    url(r'^(?P<id>\d+)/$', contacts_detail, name='contact_detail'),
]


views.py
from django.contrib.auth.decorators import user_passes_test
from django.shortcuts import render, get_object_or_404
from .models import Contact


@user_passes_test(lambda u: u.is_staff)
def contacts_list(request):
    c = {}
    contacts = Contact.objects.all()
    c['contacts'] = contacts
    return render(request, 'contacts/contacts_list.html', c)


def contacts_detail(request, id=None):
    instance = get_object_or_404(Contact, id=id)
    context = {
        "title": "Contact Details",
        "instance": instance,
    }
    return render(request, "contacts/contacts_detail.html", context)

contact_list.html
<td><a href='{{ contact.get_absolute_url }}'>{{ contact.id }}</a></td>

Any advice would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6c37dbdb-bfcc-4555-8112-abd849c78055%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment