I am working on a project that manages different servers and the whitelist associated with each server. I would like to show the associated whitelist for the server in the server's DetailView page. Right now, I have this sort of working by simply calling the Django HTML escapes for the whitelist objects in my template. This works okay but I would like to enable the end user to create/update/delete these whitelists from the respective server detail pages. Here are the two models:
#from models.py
class Server(models.Model):
server_url = models.CharField(max_length=100, unique=True)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
sw_vendor = models.CharField(max_length=20, choices=SW_VENDORS_CHOICES)
sw_version = models.CharField(max_length = 10)
pb_method = models.CharField(max_length = 20, choices = PB_METHOD_CHOICES)
date_added = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
history = HistoricalRecords()
def __str__(self):
return self.server_url
def get_absolute_url(self):
return reverse('proxyadmin-server-detail', kwargs={'pk': self.pk})
#and
class Whitelist(models.Model):
reference = models.CharField(max_length=20, unique=True)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
server = models.OneToOneField(Server, on_delete=models.CASCADE)
applications = models.ManyToManyField('Application')
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.reference
I tried creating CBVs for Whitelist and referencing it in the template but ran into a Reverse URL error filtering because I could not figure out how to filter the whitelist by any of the Server field.
Any help would be greatly appreciated! Let me know if you need to see additional code, I am not sure exactly what needs to be seen to answer my question.
EDIT
Below is some additional code for what I have.
#from views.py
class ServerDetailView(LoginRequiredMixin, DetailView):
model = Server
class WhitelistDetailView(LoginRequiredMixin, DetailView):
model = Whitelist
and:
#from urls.py
from django.urls import path
from .views import (
ServerListView,
ServerDetailView,
ServerCreateView,
ServerUpdateView,
ServerDeleteView,
WhitelistDetailView
)
from . import views
urlpatterns = [
path('', views.home, name='proxyadmin-home'),
path('customers/', views.customers, name='proxyadmin-customers'),
path('servers/', ServerListView.as_view(extra_context={'title': 'Servers', 'servers_page': "active"}), name='proxyadmin-servers'),
path('servers/<int:pk>/', ServerDetailView.as_view(extra_context={'title': 'Servers', 'servers_page': "active"}), name='proxyadmin-server-detail'),
path('servers/new/', ServerCreateView.as_view(extra_context={'title': "Servers", 'servers_page': "active", 'heading': "Create Server"}), name='proxyadmin-server-create'),
path('servers/<int:pk>/edit/', ServerUpdateView.as_view(extra_context={'title': "Servers", 'servers_page': "active", 'heading': "Edit Server"}), name='proxyadmin-server-edit'),
path('servers/<int:pk>/delete/', ServerDeleteView.as_view(extra_context={'title': "Servers", 'servers_page': "active", 'heading': "Delete Server"}), name='proxyadmin-server-delete'),
path('whitelists/<int:pk>/', WhitelistDetailView.as_view(extra_context={'title': 'Whitelists'}), name='proxyadmin-whitelist-detail'),
]
In my Server template, I am trying to just have a link to the associated whitelist as proof of concept. In the future I will want to display a table of the info from the Whitelist model with the info.
<a href="{% url 'proxyadmin-whitelist-detail' %}">View Whitelist</a>
The error I get back when I click this link is:
NoReverseMatch at /servers/1/
Reverse for 'proxyadmin-whitelist-detail' with no arguments not found. 1 pattern(s) tried: ['whitelists/(?P<pk>[0-9]+)/$']
Request Method: GET
Request URL: http://localhost:8000/servers/1/
Django Version: 3.0.5
Exception Type: NoReverseMatch