Skip to content

Instantly share code, notes, and snippets.

@dfinninger
Last active August 30, 2018 06:11
Show Gist options
  • Save dfinninger/cdcfe89c70a02fc8023aa7f8024bc344 to your computer and use it in GitHub Desktop.
Save dfinninger/cdcfe89c70a02fc8023aa7f8024bc344 to your computer and use it in GitHub Desktop.
Django Generic Views
from django.db import models
from django.urls import reverse
class Server(models.Model):
name = models.CharField(max_length=128)
ip = models.CharField(max_length=16)
rack = models.CharField(max_length=32)
hall = models.CharField(max_length=32)
data_center = models.CharField(max_length=32)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("inventory:server_detail", args=[self.id])
from django.urls import path
from . import views
app_name = 'inventory'
urlpatterns = [
path('servers/', views.ServerIndexView.as_view(), name='server_index'),
path('servers/new', views.ServerCreateView.as_view(), name='server_create'),
path('servers/<int:pk>/', views.ServerDetailView.as_view(), name='server_detail'),
path('servers/<int:pk>/edit', views.ServerEditView.as_view(), name='server_edit'),
path('servers/<int:pk>/delete', views.ServerDeleteView.as_view(), name='server_delete'),
]
from django.views import generic
from django.urls import reverse_lazy
from .models import Server
class ServerIndexView(generic.ListView):
model = Server
class ServerDetailView(generic.DetailView):
model = Server
class ServerCreateView(generic.edit.CreateView):
model = Server
fields = '__all__'
class ServerEditView(generic.edit.UpdateView):
model = Server
fields = '__all__'
class ServerDeleteView(generic.edit.DeleteView):
model = Server
success_url = reverse_lazy('inventory:server_index')
@dfinninger
Copy link
Author

MIT License

Copyright (c) 2018 Devon Finninger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment