Last active
August 12, 2021 14:25
-
-
Save artschwagerb/7930048 to your computer and use it in GitHub Desktop.
Django Forms Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from inventory.models import Computer | |
class ComputerForm(forms.ModelForm): | |
class Meta: | |
model = Computer | |
class ChromebookForm(forms.ModelForm): | |
class Meta: | |
model = Computer | |
fields = ['model','serial'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import patterns, include, url | |
from django.contrib.auth.decorators import login_required | |
import inventory.views | |
from inventory.models import * | |
urlpatterns = patterns('', | |
url(r'^$', 'inventory.views.index', name='computers-list'), | |
url(r'^dashboard', 'inventory.views.dashboard', name='computer-dashboard'), | |
url(r'^computer/(?P<computer_id>\d+)/view$', 'inventory.views.computer', name='computer-view'), | |
url(r'^computer/new$', inventory.views.CreateComputerView.as_view(),name='computers-new'), | |
url(r'^computer/(?P<pk>\d+)/edit$', login_required(inventory.views.UpdateComputerView.as_view()), name='computer-edit'), | |
url(r'^chromebook/new', inventory.views.CreateChromebookView.as_view(), name='cb-add'), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render_to_response, get_object_or_404, render | |
from django.http import HttpResponse, HttpResponseRedirect, Http404 | |
from django.template import RequestContext, loader | |
from django.core.context_processors import csrf | |
from django.core.urlresolvers import reverse | |
from django import forms | |
from django.views.generic import ( | |
ListView, | |
CreateView, | |
UpdateView, | |
DeleteView, | |
) | |
from django.contrib.auth.decorators import login_required, permission_required | |
from django.contrib.contenttypes.models import ContentType | |
from inventory.forms import * | |
class CreateComputerView(CreateView): | |
model = Computer | |
template_name = 'computer_form.html' | |
def get_success_url(self): | |
return reverse('computer-dashboard') | |
def get_context_data(self, **kwargs): | |
context = super(CreateComputerView, self).get_context_data(**kwargs) | |
context['target'] = reverse('computers-new') | |
return context | |
class UpdateComputerView(UpdateView): | |
model = Computer | |
template_name = 'computer_form.html' | |
def get_success_url(self): | |
return reverse('computer-edit', | |
kwargs={'pk': self.get_object().id} | |
) | |
def get_context_data(self, **kwargs): | |
context = super(UpdateComputerView, self).get_context_data(**kwargs) | |
context['target'] = reverse('computer-edit', | |
kwargs={'pk': self.get_object().id} | |
) | |
return context | |
class DeleteComputerView(DeleteView): | |
model = Computer | |
template_name = 'delete_computer.html' | |
def get_success_url(self): | |
return reverse('computers-list') | |
#The same idea as the create with a few more options | |
class CreateChromebookView(CreateView): | |
model = Computer | |
template_name = 'chromebook_add.html' | |
def get_form(self, form_class): | |
form = super(CreateChromebookView,self).get_form(form_class) #instantiate using parent | |
form.fields['model'].queryset = Computer_Model.objects.filter(chassis="Chromebook") | |
return form | |
def form_valid(self, form): | |
self.object = form.save(commit=False) | |
self.object.save() | |
new_comment = Comment( | |
message = "Device Created", | |
author = Person.objects.get(django_user = self.request.user), | |
content_type = ContentType.objects.get(model='Computer'), | |
object_id = self.object.id, | |
) | |
new_comment.save() | |
return HttpResponseRedirect(self.get_success_url()) | |
def get_success_url(self): | |
return reverse('computer-view', kwargs={'computer_id': self.object.id}) | |
def get_context_data(self, **kwargs): | |
context = super(CreateChromebookView, self).get_context_data(**kwargs) | |
context['target'] = reverse('cb-add') | |
return context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment