jeudi 2 avril 2015

Field from the model does not appear in the view in django app

I have an application in Django 1.4.5. I added one field there (tshirt_size) - which has a default value: size S. When I add the code in the view, I get a server error. When there is no code in the view, there is only size S (admin panel), despite the fact that I chose XL.


Here is one, added field to my model:



tshirt_size = models.CharField(choices=TSHIRT_SIZE_CHOICES, default="s", blank=True, max_length=24)


Here is my form



TSHIRT_SIZE_CHOICES = (
('s', 'S'),
('m', 'M'),
('l', 'L'),
('xl', 'XL')
)

class SubscriberForm(forms.ModelForm):
class Meta():
model = Subscriber
exclude = ['event', 'is_active']
widgets = {
'prefix': Select(choices=PREFIX_CHOICES),
'name': TextInput(),
'last_name': TextInput(),
'email': TextInput(),
'company': TextInput(),
'job_title': TextInput(),
'phone_number': TextInput(),
'tshirt_size': Select(choices=TSHIRT_SIZE_CHOICES),
}


Here is my view:



def add_subscriber(request):
"""
Receiving data via Ajax
"""
result = json.dumps({'result': 'Missing data'})
if request.is_ajax():
form = SubscriberForm(request.POST)
if form.is_valid():
email = request.POST.get('email')

if email is not None:
event_pk = request.POST.get('id')
prefix = request.POST.get('prefix')
name = request.POST.get('name')
last_name = request.POST.get('last_name')
company = request.POST.get('company')
job_title = request.POST.get('job_title')
phone_number = request.POST.get('phone_number')

try:
event = Event2.objects.get(pk=event_pk)
except Event2.DoesNotExist:
result = json.dumps({'result': 'No event'})
return HttpResponse(result, content_type='application/json')

if Subscriber.objects.filter(
email__iexact=email, event=event).exists():
result = json.dumps({'result': 'Already Registered'})
return HttpResponse(result, content_type='application/json')

subscriber = Subscriber(
event=event,
prefix=prefix,
name=name,
last_name=last_name,
email=email,
company=company,
job_title=job_title,
phone_number=phone_number,)
subscriber.save()
result = json.dumps({'result': 'ok'})
return HttpResponse(result, content_type='application/json')

return HttpResponse(result, content_type='application/json')

Aucun commentaire:

Enregistrer un commentaire