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
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
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
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:-
<!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:-
<!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:-
<!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:-
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