I am trying to reverse to the Detail View after the Update View and to List View after Delete View. How can I implement this. I am getting an error reverse not defined.
Thanks
#Contents of Models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Profile(models.Model):
OPTIONS_PROFILE_TYPE = (
('single', 'Single'),
('family', 'Family'),
)
OPTIONS_LOCATIONS = (
('USA', 'USA'),
('Canada', 'Canada'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE, help_text='User Name')
display_name = models.CharField(null=True, max_length = 16, help_text='Optional Display Name' )
profile_type = models.CharField(null=True, choices=OPTIONS_PROFILE_TYPE, max_length=10)
location = models.CharField(null=True, max_length = 30, choices=OPTIONS_LOCATIONS, help_text='Optional Location')
biography = models.CharField(null=True, blank=True, max_length = 500, help_text='Optional Biography')
# Metadata
class Meta:
ordering = ['profile_type']
# Methods
def get_absolute_url(self):
#Returns the url to access a particular instance of MyModelName.
return reverse('profile-detail', args=[str(self.id)])
def __str__(self):
#String for representing the MyModelName object (in Admin site etc.).
return self.user.username
#Contents of Views.py
from datetime import datetime
from django.shortcuts import render
from django.urls import path
from django.http import HttpRequest
from .models import Profile
from django.views.generic import FormView, CreateView, DetailView, UpdateView, DeleteView, ListView
from django.shortcuts import redirect
from django.urls import reverse
class ProfileListView(ListView):
model = Profile
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(ProfileListView, self).get_context_data(**kwargs)
# Create any data and add it to the context
context['title'] = 'Users'
context['message'] = 'given below is a list of all users'
return context
class ProfileCreateView(CreateView):
model = Profile
fields = ['user', 'display_name', 'profile_type', 'location', 'biography']
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(ProfileCreateView, self).get_context_data(**kwargs)
# Create any data and add it to the context
context['title'] = 'Create User'
context['message'] = 'please use the form below to create the user'
return context
class ProfileDetailView(DetailView):
model = Profile
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(ProfileDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
context['title'] = 'User Detail'
context['message'] = 'given below are the details of the user'
return context
class ProfileUpdateView(UpdateView):
model = Profile
fields = ['user', 'display_name', 'profile_type', 'location', 'biography']
def get_success_url(self):
return reverse('profile-detail', kwargs={'pk' : self.object.pk})
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(ProfileUpdateView, self).get_context_data(**kwargs)
# Create any data and add it to the context
context['title'] = 'Update User'
context['message'] = 'please use the form below to update the user'
return context
class ProfileDeleteView(DeleteView):
model = Profile
CONTENTS OF URLS.PY
from datetime import datetime
from django.urls import path
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView
from app import forms, views
from app.views import ProfileListView, ProfileDetailView, ProfileCreateView, ProfileUpdateView
urlpatterns += [
path('profiles/', views.ProfileListView.as_view(), name='profiles'),
path('profile/<int:pk>', views.ProfileDetailView.as_view(), name='profile-detail'),
path('create/', views.ProfileCreateView.as_view(), name='create'),
path('edit/<int:pk>/', views.ProfileUpdateView.as_view(), name='update'),
The error I am getting is - Reverse Not Defined when using the update view. I tried looking up and the solution which I have: Create View reverse to DetailView requires me to migrate the get success url method to the views section. Is it possible to avoid this?