# Regarding getabsoluteurl function in model based views

**URL:** <https://forum.djangoproject.com/t/regarding-getabsoluteurl-function-in-model-based-views/8667>\
**Category:** Using Django\
**Created:** [July 7, 2021, 5:01pm UTC](https://forum.djangoproject.com/t/regarding-getabsoluteurl-function-in-model-based-views/8667 "2021-07-07T17:01:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rishabh101](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rishabh101/32/4325_2.png) [@rishabh101](https://forum.djangoproject.com/u/rishabh101)\
**Post date:** [July 7, 2021, 5:01pm UTC](https://forum.djangoproject.com/t/regarding-getabsoluteurl-function-in-model-based-views/8667/1 "2021-07-07T17:01:10Z")

</div>

I am using model based views like ListView and DetailedView, and for performing CRUD operations i am facing problem in get\_absolute\_url function

Models.py file

```auto
from django.urls import reverse
# Create your models here.
class Company(models.Model):
    name=models.CharField(max_length=128)
    location=models.CharField(max_length=64)
    ceo=models.CharField(max_length=64)

def get_absolute_url(self):
    return reverse('detail',kwargs={'pk':self.pk})

```

urls.py

```auto
from django.contrib import admin
from django.urls import path
from testapp import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', views.CompanyListView.as_view()),
    path('<int:pk>', views.CompanyDetailView.as_view(),name='detail'),
    path('create/', views.CompanyCreateView.as_view()),

]

```

views.py

```auto
from django.shortcuts import render
from testapp.models import Company
from django.views.generic import ListView,DetailView,CreateView,UpdateView,DeleteView
# Create your views here.
from django.urls import reverse
class CompanyListView(ListView):
    model=Company

class CompanyDetailView(DetailView):
    model=Company

class CompanyCreateView(CreateView):
    model=Company
    fields=('name','location','ceo')

```

In the company\_detail html:-

```auto
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{%block body_block%}
<h1>{{company.name}} Information</h1>
<h2>Location:{{company.location}}</h2>
<h2>Name of CEO:{{company.ceo}}</h2>
{%endblock%}

```

In the company\_form.html:-

```auto
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{%block body_block%}

<h1>Employee Insert Info Form</h1><hr>
<form method="post">
  {{form.as_p}}
  {%csrf_token%}

  <input type="submit" class="btn btn-lg btn-primary" value="Insert">
</form>

{%endblock%}

```

In the company\_list.html:-

```auto
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{%block body_block%}

<h1>List of all companies</h1><hr>
{%for company in company_list%}
<h2><a href="/{{company.id}}">{{company.name}}</a></h2>
{%endfor%}
{%endblock%}

```

I am getting the following error:-

```auto
Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 116, in get_success_url
    url = self.object.get_absolute_url()
AttributeError: 'Company' object has no attribute 'get_absolute_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 126, in form_valid
    return super().form_valid(form)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 57, in form_valid
    return HttpResponseRedirect(self.get_success_url())
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 118, in get_success_url
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.
[07/Jul/2021 19:20:14] "POST /create/ HTTP/1.1" 500 100006

```

i have already defined get\_absolute\_url method in the models.py ,still I am getting this error, please help me with this

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 7, 2021, 5:16pm UTC](https://forum.djangoproject.com/t/regarding-getabsoluteurl-function-in-model-based-views/8667/2 "2021-07-07T17:16:04Z")

</div>

Assuming the forum hasn’t messed up the formatting of your code that you posted, the issue is that the get\_absolute\_url method you’ve defined is not part of the Company class. (It’s not indented)

> [@rishabh101](#):
>
> ```auto
> class Company(models.Model):
> name=models.CharField(max_length=128)
> location=models.CharField(max_length=64)
> ceo=models.CharField(max_length=64)
> 
> def get_absolute_url(self):
> return reverse('detail',kwargs={'pk':self.pk})
> 
> ```
