{{ field.label_tag }} |
{{ field }}
{% if field.errors %}
{% for error in field.errors %}
{{ field.label_tag }}: {{ error }}
{% endfor %}
{% endif %}
|
{% endfor %}
```
---
## Form: Validation is its own Talk
example of cross field validation
```python
class SharedReportRecordForm(forms.ModelForm):
class Meta:
model = SharedReportRecord
def clean(self):
from_email = self.cleaned_data.get('from_email')
to_email = self.cleaned_data.get('to_email')
if from_email and to_email:
if from_email == to_email:
msg = 'Why are you sending email to yourself'
self._errors["to_email"] = self.error_class([msg])
self._errors["from_email"] = self.error_class([msg2])
msg2 = 'To and From email cannot be the same'
return self.cleaned_data
```
---
## Form validation: another peak
```python
if request.POST:
* f = SharedReportRecordForm(request.POST)
* if f.is_valid():
print(f)
```
```javascript
{'to_email': u'raman_prasad@harvard.edu', 'from_name': u'raman', 'report_month': datetime.date(2014, 8, 1), 'from_email': u'raprasad@gmail.com'}
```
---
## Send an HTML Email
```python
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email])
msg.attach_alternative(html_content, "text/html")
msg.send()
```
---
## API: Tastypie Example
```html
/api/v1/seizure-location-type/...```
```python
#apps/clanlabs/api.py
*class SeizureLocationTypeResource(ModelResource):
class Meta:
queryset = SeizureLocationType.objects.all()
resource_name = 'seizure-location-type'
excludes = [ 'created', 'modified']
```
```python
#urls.py
from tastypie.api import Api
*from apps.clanlabs.api import SeizureLocationTypeResource, ClanLabReportResource
v1_api = Api(api_name='v1')
v1_api.register(SeizureLocationTypeResource())
v1_api.register(ClanLabReportResource())
urlpatterns += patterns('',
(r'^api/', include(v1_api.urls)),
)```