Created
April 28, 2013 12:05
-
-
Save bootandy/5476693 to your computer and use it in GitHub Desktop.
django class based views sample code.
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
# -------- models -------------- | |
class CheckStart(models.Model): | |
check_start = models.OneToOneField(Checklist, related_name="start") | |
Registration = models.BooleanField(default=False) | |
Make_an_offer = models.BooleanField(default=False) | |
Offer_accepted = models.BooleanField(default=False) | |
class CheckStartForm(ModelForm): | |
class Meta: | |
model = CheckStart | |
exclude = ('check_start',) | |
# -------- END models -------------- | |
# ---------- Handy :-) : ---------- | |
# from django.forms.models import model_to_dict | |
# model_to_dict(cc) | |
class Page1Update(UpdateView): | |
model = Checklist | |
form_class = ChecklistForm | |
success_url = "/house/checklist_list/" | |
def get(self, request, *args, **kwargs): | |
cl = Checklist.objects.get(pk=kwargs['pk']) | |
# import pdb | |
# pdb.set_trace() | |
try: | |
cs = CheckStart.objects.get(check_start=cl) | |
except Exception: | |
cs = CheckStart.objects.create(check_start=cl) | |
cs.save() | |
csf = CheckStartForm(instance=cs, initial={'check_start': cl}) | |
return render_to_response('house/start.html', {'form': csf}, RequestContext(request)) | |
def post(self, request, *args, **kwargs): | |
cs = CheckStart.objects.get(check_start=Checklist.objects.get(pk=kwargs['pk'])) | |
cs.Offer_accepted = ('Offer_accepted' in request.POST and request.POST['Offer_accepted'] == 'on') | |
cs.Make_an_offer = ('Make_an_offer' in request.POST and request.POST['Make_an_offer'] == 'on') | |
cs.Registration = ('Registration' in request.POST and request.POST['Registration'] == 'on') | |
cs.save() | |
return HttpResponseRedirect('/house/checklist_list/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment