我正在尝试使用 Django 1.4.6 项目来跟踪销售线索。我希望这对移动设备友好,因此我将 Twitter Bootstrap(仍在 2.3.2 版上)与django-crispy-forms一起使用。根据这篇文章,做电子邮件字段<input type="email" autocapitalize="off" autocorrect="off">
的最佳方法是,做日期字段的最佳方法是<input type="date">
。默认情况下,这些属性都没有在 Django 中实现,我想知道如何最好地实现它们。这是相关代码(简化):
模型.py
from django.db import models
class Lead(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField(blank=True, null=True)
initial_contact_date = models.DateField()
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
视图.py
from django.core.urlresolvers import reverse
from django.views.generic import CreateView, ListView, UpdateView
from .models import Lead
class LeadAdd(CreateView):
model = Lead
def get_context_data(self, **kwargs):
context = super(LeadAdd, self).get_context_data(**kwargs)
context['title'] = 'Add a Lead'
return context
def get_success_url(self):
return reverse('lead_list')
class LeadEdit(LeadAdd, UpdateView):
def get_context_data(self, **kwargs):
context = super(LeadEdit, self).get_context_data(**kwargs)
context['title'] = 'Edit a Lead'
return context
class LeadList(ListView):
model = Lead
网址.py
from django.conf.urls import patterns, url
from .views import *
urlpatterns = patterns('',
url(r'^$', view=LeadList.as_view(), name='lead_list'),
url(r'^add/$', view=LeadAdd.as_view(), name='lead_add'),
url(r'^edit/(?P<pk>[\d]+)/$', view=LeadEdit.as_view(), name='lead_edit'),
)
Lead_form.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="page-header">
<h1>{{ title }}</h1>
</div>
<form action="." method="post" class="form-horizontal">
{% csrf_token %}
{{ form|crispy}}
<div class="form-actions">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
{% endblock content %}