【问题标题】:Field from the model does not appear in the view in django app模型中的字段未出现在 django 应用程序的视图中
【发布时间】:2015-06-07 09:37:51
【问题描述】:

我在 Django 1.4.5 中有一个应用程序。 我在那里添加了一个字段(tshirt_size)——它有一个默认值:size S。 当我在视图中添加代码时,出现服务器错误。当视图中没有代码时,尽管我选择了 XL,但只有尺寸 S(管理面板)。

这是一个,在我的模型中添加了一个字段:

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

这是我的表格

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),
        }

这是我的看法:

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')

【问题讨论】:

    标签: python django forms select view


    【解决方案1】:

    整个视图是不必要的。正如您已经被告知的,您应该在 is_valid 块内调用form.save()。这会自动获取您的所有字段并为您创建一个模型实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多