0
django 양식 마법사 sessionwizardview
을 사용하여 작성된 다중 단계 양식이 있습니다. 다음 및 이전 버튼이 추가되었지만, 문제가 있습니다. 단계를 클릭하고 이전을 클릭하면 먼저 채워지는 양식을 요청한 다음 계속 진행하고 이전 버튼을 클릭하여 이전 단계로 이동합니다. 나는 내 문제와 관련이있는 것을 찾지 못해서 우둔하다. 나는 무엇인가 놓치고 있는가? 여기 양식 마법사를 사용하여 양식의 유효성을 검사하지 않고 이전 단계로 이동하는 방법
from django import forms
from django.utils.translation import gettext as _
from addresses.forms import AddressForm, InvoiceAddressForm
from core.api import NcpAPI
from django_countries import countries
api = NcpAPI()
CHOICES=[('0','Pay by card'), ('1','Invoice')]
class RegistrationForm(forms.Form):
title = 'registration'
first_name = forms.CharField(label=_("First Name"), widget=forms.TextInput(attrs={'class':'form-text required'}))
last_name = forms.CharField(label=_("Last Name"), widget=forms.TextInput(attrs={'class':'form-text required'}))
registration_company = forms.CharField(label=_("Company"), widget=forms.TextInput(attrs={'class':'form-text required'}))
phone = forms.CharField(label=_("Phone"), widget=forms.TextInput(attrs={'class':'form-text required'}))
email = forms.EmailField(label=_("Email"), widget=forms.TextInput(attrs={'class':'form-text required'}))
def clean_email(self):
email = self.cleaned_data.get('email')
if api.is_username_taken(email):
raise forms.ValidationError(_('Email is in use'))
return email
class AddressesForm(InvoiceAddressForm, AddressForm):
title = 'addresses'
class PaymentForm(forms.Form):
title = 'payment'
payment_method = forms.ChoiceField(label = _("Payment Options"), choices=CHOICES, widget=forms.RadioSelect(attrs={'class':'form-text required'}), required = True, initial = "1")
class ConfirmationForm(forms.Form):
title = 'confirm'
그리고 여기 내 세션 마법사 클래스입니다 :
class SubscriptionWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES.get(self.steps.current)]
extra_data = {}
def get_context_data(self, form, **kwargs):
pp = pprint.PrettyPrinter(indent=4)
context = super(SubscriptionWizard, self).get_context_data(form=form, **kwargs)
context.update(self.extra_data)
data = self.get_all_cleaned_data()
context['all_data'] = {"product":self.kwargs['product']}
# if self.steps.current == 'addresses':
# print ' yes its addresses %s' % data.get('company')
# context['all_data'].update({"reg_company":data.get('company')})
if self.steps.current in ('payment', 'confirm'):
if data[u'invoice_name'] != '':
p = Prices.objects.filter(name = self.kwargs['product'], currency = str(self.getCurrencyCode(str(data[u'invoice_country']))))
else:
p = Prices.objects.filter(name = self.kwargs['product'], currency = str(self.getCurrencyCode(data.get('country'))))
context['all_data']['product_price'] = p[0].price
context['all_data']['product_currency'] = p[0].currency
if data.get('invoice_name'):
currency_country = data.get('invoice_country')
else:
currency_country = data.get('country')
if self.steps.current == 'confirm':
p = Prices.objects.filter(name = self.kwargs['product'], currency = str(self.getCurrencyCode(data.get('country'))))
context['all_data']['product_price'] = p[0].price
context['all_data']['product_currency'] = p[0].currency
if data:
# pp.pprint(data)
context['all_data'].update(data)
# pp.pprint(context['all_data'])
return context
def get_form_initial(self, step):
initial = self.initial_dict.get(step, {})
if 'profiler' in self.request.session and step in ('registration', 'address', 'payment'):
profiler = self.request.session.get('profiler')
data = {}
if step == 'registration':
# pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(profiler.user.account.account)
# print profiler.user.account
data = {'email': profiler.user.account.email ,
'first_name':profiler.user.account.firstName if profiler.user.account.account.firstName != '' else '',
'last_name':profiler.user.account.lastName,
'phone': profiler.user.account.phone1 if profiler.user.account.account.firstName != '' else ''}
initial.update(data)
return initial
def get_form(self, step=None, data=None, files=None):
form = super(SessionWizardView, self).get_form(step, data, files)
if hasattr(form, 'initialize_wizard'):
form.initialize_wizard(self)
return form
def getCurrencyCode(self, countryCode):
continent = transformations.cca_to_ctn(countryCode)
# print continent
if str(countryCode) == 'NO':
return 'NOK'
if str(countryCode) == 'GB':
return 'GBP'
if (continent == 'Europe') or (continent == 'Africa'):
return 'EUR'
return 'USD'
def done(self, form_list, **kwargs):
pp = pprint.PrettyPrinter(indent=4)
import hashlib
data = dict(('%s_%s' % (form.prefix,k),v) for form in form_list for k, v in form.cleaned_data.items())
# print 'print data ....'
# print ''
# print ''
# pp.pprint(data)
# print ''
# print ''
# print ''
# print '--------------'
# print data
full_name = "%s %s" % (data['registration_first_name'],data['registration_last_name'])
data['product'] = kwargs['product']
dumps = simplejson.dumps(data)
data['country_label']=unicode(fields.Country(data['addresses_country']).name)
print data
if data.get('invoice_name'):
currency_country = data.get('addresses_invoice_country')
else:
currency_country = data.get('addresses_country')
currencyCode = self.getCurrencyCode(currency_country)
prices = Prices.objects.filter(name = kwargs['product'], currency = str(currencyCode))
data['product_price'] = prices[0].price
data['product_currency'] = str(currencyCode)
# print currencyCode
include_archive = 'standard' in kwargs.values()
# Register.
# print 'print data before registering the product ....'
# print ''
# print ''
# pp.pprint(data)
# print ''
# print ''
# print ''
# print '--------------'
result = api.subscribe_to_product(subscribe_to_archive=include_archive, **data)
if 'errorCode' in result:
messages.add_message(self.request, messages.ERROR, _('The registration was not successfull.'))
return render(self.request, 'subscription_product/failed.html', {'clean_data': data})
print '--------'
cs_message = render_to_string(
'subscription_product/email/registered_cs.html', {'data':data})
print api.email(settings.EMAIL_CS_NAME, settings.EMAIL_CS_EMAIL, "Registration for %s" % data['product'], cs_message)
# Save subscription.
s = SubscriptionInfo(subscriptionId = str(result[u'subscriptionId']), customerNumber = result[u'customerNumber'],subscriptionType = str(data['product']), currency = str(currencyCode))
s.save()
if int(data['payment_payment_method']) == 1:
# Sends activation email.
token = api.create_token(7200, 99,'productSub', data['registration_email']).get('tokenValue', '')
activation_url = Site.objects.get_current().domain + reverse("activation_home", kwargs={'token':token})
# activation_url = 'http://localhost:8080' + reverse("activation_home", kwargs={'token':token})
# full_name = '%s %s' % (data.get('registration_first_name'), data.get('registration_last_name'))
customer_message = render_to_string(
'subscription_product/email/registered_customer.html', {'activation_url':activation_url})
print api.email("%s %s" % (data['registration_first_name'], data['registration_last_name']), data['registration_email'], "Your Tradewindsnews.com order registration", customer_message)
#SEND EMAIL TO SALES ON SUCCESS
sales_message = render_to_string(
'subscription_product/email/invoice_sales_success.html',
{'customer_name': full_name,
'account': data,
'alternative_invoice_company':data['addresses_invoice_company'],
'alternative_invoice_street':data['addresses_invoice_street'],
'alternative_invoice_city':data['addresses_invoice_city'],
'alternative_invoice_postal_code':data['addresses_invoice_postal_code'],
'alternative_invoice_country':data['addresses_invoice_country'],
'payment_date': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'amount': float(prices[0].price),
'currency' : currencyCode,
'transactionid' :'',
'authorizationid' : '',
'confirmation_number': ''})
api.email("%s %s" % (data['registration_first_name'], data['registration_last_name']), settings.EMAIL_SALES, "New Subscription Order", sales_message)
return render(self.request, 'subscription_product/receipt.html', {'clean_data': data})
else:
site_url = "http://%s" % Site.objects.get(name='secondary')
# site_url = "http://localhost:8000"
# dumps = simplejson.dumps(data)
p = PaymentBeforeDump(dump = dumps, subscriptionInfo = s)
p.save()
# prices = Prices.objects.get(name = kwargs['product'])
return HttpResponseRedirect(
Register(
p_request_Order_Amount = int(float(prices[0].price)*100),
p_request_Order_CurrencyCode = currencyCode,
p_request_Order_OrderNumber = hashlib.md5("foobar" + str(time.time())).hexdigest(),
p_request_Terminal_RedirectUrl = site_url + reverse("subscription_product_payment_return",kwargs={'amount':int(float(prices[0].price)*100), 'currency': str(currencyCode), 'subscription': kwargs['product'], 'id':p.id}),
p_request_TransactionReconRef = 'tw-random',
)
)
그리고 여기에 하나의 템플릿입니다 :
이{% extends "base.html" %}
{% load url from future %}
{% load i18n %}
{% load subscription_product_tags %}
{% block content %}
<section class="topsection group">
<div class="col span_3_of_3 first-child last-child">
<div class="order">
<ol class="steps">
<li class="done">Subscription</li>
<li class="done">Details</li>
<li class="done">Delivery</li>
<li class="active">Payment</li>
<li class="queue">Confirm</li>
<li class="queue">Receipt</li>
</ol>
<div class="box">
<section class="section group first-child">
<h1>Payment Summary</h1>
Please review the following details for this transaction.
</section>
<section class="topsection group">
<table border="1">
<tr>
<th>
Description
</th>
<th>
Item Price
</th>
</tr>
<tr>
{% comment %}get the pricing and other product related info{% endcomment %}
{{ all_data|product_price_info|safe }}
</tr>
</table>
</section>
<form action="" method="post">{% csrf_token %}
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
<section class="topsection group">
{{ wizard.form.payment_method.label_tag }}
{{wizard.form.payment_method}}
{{ wizard.form.payment_method.errors}}
</section>
{%endif %}
<section class="section group last-child">
<div class="col span_3_of_3 first-child last-child">
<fieldset class="form-actions">
<!-- <a class="backbutton" onClick="goPrevious()">Previous</a> -->
{# <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "Previous" %}</button> #}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "Previous" %}</button>
<input type="submit" value="Next">
</fieldset>
</div>
</section>
</form>
</div>
</div>
</div>
</section>
{% endblock %}
{% block customerservice %}{% endblock %}
{% block footer %}{% endblock %}
저는 formwizard에 익숙하지 않으므로 generell 아이디어 일뿐입니다. 뒤로 버튼은 type = "submit"입니다. 이것은 양식 제출 및 유효성 검사를 트리거합니다. 아마도 제출하지 않고 "backlink"를 제공 할 수 있습니다 ... – Jingo
하지만 이전 단계에서 빈 필드를 얻습니다. 앞으로. – Maverick
자바 스크립트 유효성 검사가 있습니까? – Rohan