def demo_add(request): context = RequestContext(request) context_dict ={} t = get_team(request.user) add_isteam_to_context_dict(context_dict, t) added = False t = get_team(request.user) if t: if request.method == 'POST': demo_form = DemoForm(data=request.POST) if demo_form.is_valid(): demo = demo_form.save(commit=False) demo.team = t if 'screenshot' in request.FILES: demo.logo = request.FILES['screenshot'] demo.save() added = True return HttpResponseRedirect('/showcase/team/'+str(t.id)+'/') else: print demo_form.errors else: demo_form = DemoForm() # Render the template depending on the context. context_dict['demo_form'] = demo_form context_dict['added'] = added return render_to_response( 'showcase/demo_add.html', context_dict, context) else: return HttpResponse('You need to be a team to add a demo.')
def contact(demo): form = ContactForm() form2 = DemoForm() demo = False if demo == '0' else True if request.method == 'POST': if form and form.validate() == False and demo == False or form2 and form2.validate() == False and demo == True: error_message = gettext('All fields are required and e-mail must be valid') flash(error_message) return render_template('contact.html', **locals()) elif form2 and form2.validate() == True: msg = Message("Demo Request", sender='*****@*****.**', recipients=['*****@*****.**']) msg.body = """ From: %s <%s> %s """ % (form.name.data, form.email.data, form.message.data) mail.send(msg) success = True return render_template('contact.html', **locals()) elif form and form.validate() == True: msg = Message(form.subject.data, sender='*****@*****.**', recipients=['*****@*****.**']) msg.body = """ From: %s <%s> %s """ % (form.name.data, form.email.data, form.message.data) mail.send(msg) success = True return render_template('contact.html', **locals()) else: return render_template('contact.html', **locals()) elif request.method == 'GET': return render_template('contact.html', **locals())
def demoform(request): ''' This view handles the form submission. ''' if request.POST: ''' This part handles the real form submission. In real life, you would save the data from the form, or something like that. ''' form = DemoForm(request.POST) if form.is_valid(): return HttpResponse("DemoForm submit succeeded!") else: return HttpResponse("DemoForm validation failed. Make sure you fill out all fields.") else: ''' This part handles generating a form for the user ''' params = { 'formid' : 'createform', 'submiturl' : "demoform", 'btntxt' : "Submit form", 'jsurl' : 'djhform.js' } params["form"] = DemoForm().ajaxRequest(request) # Magic! if request.GET: ''' This is an AJAX request with form parameters set. Generate a form snippet here, so don't use the entire template ''' return HttpResponse(params["form"].as_table(), content_type="application/xhtml") else: ''' Generate a brand new form ''' return render_to_response('hierform/formtest.html', params)
def demoui(): form = DemoForm() if form.validate_on_submit(): #print(request.form.to_dict()) args = copy.deepcopy(request.form.to_dict()) del args['csrf_token'] return redirect(url_for('demo_app', **args)) return render_template('demo.html', title='nexo', form=form)
def view2(request): # country = Canada, state = Ontario form = DemoForm(request.GET) self.assertEqual(len(form.fields["country"].choices.queryset), 3) self.assertEqual(len(form.fields["state"].choices.queryset), 2) self.assertEqual(len(form.fields["city"].choices.queryset), 3) self.assertEqual(len(form.fields["neighborhood"].choices.queryset), 0) self.assertFalse(form.is_valid()) form2 = form.ajaxRequest(request) self.assertEqual(len(form2.fields["country"].choices.queryset), 3) self.assertEqual(len(form2.fields["state"].choices.queryset), 2) self.assertEqual(len(form2.fields["city"].choices.queryset), 3) self.assertEqual(len(form2.fields["neighborhood"].choices.queryset), 0) self.assertFalse(form2.is_valid()) with self.assertRaises(Exception): form2.save() # ajax forms are not meant to be saved
def view4(request): # country = Canada, state = Ontario, city=San Francisco # Should ignore San Francisco and show valid choices for Ontario form = DemoForm(request.GET) self.assertEqual(len(form.fields["country"].choices.queryset), 3) self.assertEqual(len(form.fields["state"].choices.queryset), 2) self.assertEqual(len(form.fields["city"].choices.queryset), 3) self.assertEqual(len(form.fields["neighborhood"].choices.queryset), 7) #should be 0, this is invalid self.assertFalse(form.is_valid()) form2 = form.ajaxRequest(request) self.assertEqual(len(form2.fields["country"].choices.queryset), 3) self.assertEqual(len(form2.fields["state"].choices.queryset), 2) self.assertEqual(len(form2.fields["city"].choices.queryset), 3) self.assertEqual(len(form2.fields["neighborhood"].choices.queryset), 0) self.assertFalse(form2.is_valid()) with self.assertRaises(Exception): form2.save() # ajax forms are not meant to be saved
def view3(request): # country = Canada, state = Ontario, city=Toronto, neighborhood=North York # This is the one fully valid case form = DemoForm(request.GET) self.assertEqual(len(form.fields["country"].choices.queryset), 3) self.assertEqual(len(form.fields["state"].choices.queryset), 2) self.assertEqual(len(form.fields["city"].choices.queryset), 3) self.assertEqual(len(form.fields["neighborhood"].choices.queryset), 3) self.assertTrue(form.is_valid()) form2 = form.ajaxRequest(request) self.assertEqual(len(form2.fields["country"].choices.queryset), 3) self.assertEqual(len(form2.fields["state"].choices.queryset), 2) self.assertEqual(len(form2.fields["city"].choices.queryset), 3) self.assertEqual(len(form2.fields["neighborhood"].choices.queryset), 3) self.assertFalse(form2.is_valid()) with self.assertRaises(Exception): form2.save() # ajax forms are not meant to be saved
def demo_add(request): context = RequestContext(request) context_dict = {} t = get_team(request.user) add_isteam_to_context_dict(context_dict, t) added = False t = get_team(request.user) if t: if request.method == 'POST': demo_form = DemoForm(data=request.POST) if demo_form.is_valid(): demo = demo_form.save(commit=False) demo.team = t if 'screenshot' in request.FILES: demo.logo = request.FILES['screenshot'] demo.save() added = True return HttpResponseRedirect('/showcase/team/' + str(t.id) + '/') else: print demo_form.errors else: demo_form = DemoForm() # Render the template depending on the context. context_dict['demo_form'] = demo_form context_dict['added'] = added return render_to_response('showcase/demo_add.html', context_dict, context) else: return HttpResponse('You need to be a team to add a demo.')
def demo_edit(request, demoid=None): """ Thanks to: http://wiki.ddenis.com/index.php?title=Django,_add_and_edit_object_together_in_the_same_form """ context = RequestContext(request) t = get_team(request.user) context_dict ={} add_isteam_to_context_dict(context_dict, t) edit = False if t: if demoid: demo = get_object_or_404(Demo, pk=demoid) edit = True print demo, demo.id, demo.url if demo.team.user != request.user: return HttpResponseForbidden() else: demo = Demo(team=t) #Could use this instead -> if request.POST: if request.method == 'POST': demo_form = DemoForm(request.POST, request.FILES, instance=demo) if demo_form.is_valid(): if 'screenshot' in request.FILES: demo.logo = request.FILES['screenshot'] demo_form.save() return HttpResponseRedirect('/showcase/team/'+str(t.id)+'/') else: print demo_form.errors else: demo_form = DemoForm(instance=demo) # Render the template depending on the context. return render_to_response( 'showcase/demo_add.html', {'demo_form': demo_form, 'demoid':demoid,'edit':edit}, context) else: return HttpResponse('You need to be a team to add or edit a demo.')
def demo_edit(request, demoid=None): """ Thanks to: http://wiki.ddenis.com/index.php?title=Django,_add_and_edit_object_together_in_the_same_form """ context = RequestContext(request) t = get_team(request.user) context_dict = {} add_isteam_to_context_dict(context_dict, t) edit = False if t: if demoid: demo = get_object_or_404(Demo, pk=demoid) edit = True print demo, demo.id, demo.url if demo.team.user != request.user: return HttpResponseForbidden() else: demo = Demo(team=t) #Could use this instead -> if request.POST: if request.method == 'POST': demo_form = DemoForm(request.POST, request.FILES, instance=demo) if demo_form.is_valid(): if 'screenshot' in request.FILES: demo.logo = request.FILES['screenshot'] demo_form.save() return HttpResponseRedirect('/showcase/team/' + str(t.id) + '/') else: print demo_form.errors else: demo_form = DemoForm(instance=demo) # Render the template depending on the context. return render_to_response('showcase/demo_add.html', { 'demo_form': demo_form, 'demoid': demoid, 'edit': edit }, context) else: return HttpResponse('You need to be a team to add or edit a demo.')