4

我正在尝试使用 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 %}
4

2 回答 2

7

使用字段模板来执行此操作(请参阅 Field 上的文档):

Field('email', template="custom-email.html")

或创建自定义小部件。您可以使用一个抽象类,但现有的预定义小部件之一应该可以工作:

# widgets.py
from django.forms.widgets import TextInput

class EmailInput(TextInput):
    input_type = 'email'

因此,在您看来,它可能看起来像这样:

class LeadAdd(CreateView):
    model = Lead
    form_class = LeadAddForm
    ...

然后该 LeadAddForm 类将定义您的自定义小部件:

from . import widgets

LeadAddForm(forms.Form):
    email = forms.CharField(
        ...
        widget = widgets.EmailInput,
        ...
    )

或者您可以在init中设置小部件:

class LeadAddForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(LeadAddForm, self).__init__(*args, **kwargs)

        self.fields['email'].widget = widgets.EmailInput()

您应该能够使用清晰的表单配置设置额外的属性(autocapitalize="off" autocorrect="off"):

Field('email', autocapitalize="off", autocorrect="off")
于 2013-09-08T04:17:57.450 回答
6

更简单的选择:

forms.py

class LeadForm(forms.ModelForm):
    class Meta:
        model = Lead

    def __init__(self, *args, **kwargs):
        super(LeadForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.layout = Layout(
             Fieldset('Enter the following information',
                      'name',
                      Field('email', type="email",
                                     autocapitalize="off",
                                     autocorrect="off"),
                      Field('initial_contact_date', type="date")),
             FormActions(
                Submit('save', 'Create Lead'),
                Button('cancel', 'Cancel')
            )
        )

在你的views.py

class LeadAdd(CreateView):
    model = Lead
    success_url = reverse('lead_list')
    form_class = LeadForm

    def get_context_data(self, **kwargs):
        context = super(LeadAdd, self).get_context_data(**kwargs)
        context['title'] = 'Add a Lead'
        return context

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

在您的模板中:

{% extends 'base.html' %}

{% load crispy_forms_tags %}

{% block content %}
    <div class="page-header">
        <h1>{{ title }}</h1>
    </div>

        {% crispy form %}

{% endblock content %}
于 2013-09-08T05:18:05.227 回答