Django ModelForm有一个隐藏的输入 [英] Django ModelForm to have a hidden input

查看:105
本文介绍了Django ModelForm有一个隐藏的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有我的TagStatus模型。我试图为它制作一个ModelForm。但是,我的表单要求使用{{tag.name}}填充隐藏的输入。我一直在浏览文档,我不知道如何使标签字段成为隐藏的输入。也许一个ModelForm不是要去的?



models.py:

  class TagStatus(models.Model):
user = models.ForeignKey(User,null = True,unique = True)
status = models.CharField(max_length = 2,choices = tag_statuses)
tag = models.ForeignKey(Tag,null = True,blank = True)

def __unicode __(self):
return self.status

def save(self,* args,** kwargs):
super(TagStatus,self).save(* args,** kwargs)

class TagStatusForm (modelForm):
class Meta:
model = TagStatus
fields =('status','tag')
widgets = {
'select'
'tag':???
}

django views.py:

  @login_required 
def标签(请求):
all_tags = Tag.objects.all()
上下文= base_context(request)
if request.method =='POST':
if'status_check'in request.POST:
status_form = TagStatusForm(request.POST)
#if request.is_ajax():
如果status_form.is_valid():
status_form.save()
response = simplejson.dumps({status:成功更改状态})
else:
response = simplejson.dumps({status:Error})
return HttpResponse(response,mimetype ='application / json')
status_form = TagStatusForm()
上下文['status_form'] = status_form
上下文['all_tags'] = all_tags
返回render_to_response('tags / tags.html',context,context_instance = RequestC ontext(request))

模板:

  {%for tag in all_tags%} 
....
< form class =niceid =status-formmethod =POST行动= >
{%csrf_token%}
< input type =hiddenname =status_check/>
< input type ='hidden'name =tagvalue ={{tag.name}}/>
{{status_form.status}}
< / form>
...
{%endfor%}

通过django ModelForm进行隐藏输入,然后通过模板填充它。

解决方案

要在ModelField中创建一个隐藏的字段字段,使用HiddenInput小部件。 ModelForm对所有字段使用一个明智的默认窗口小部件,您只需要在构造对象时重写它。

  class TagStatusForm (forms.ModelForm):
class Meta:
model = TagStatus
widgets = {'tag':forms.HiddenInput()}
pre>

So I have my TagStatus model. I'm trying to make a ModelForm for it. However, my form requires that the hidden input be populated with the {{ tag.name }}. I've been looking through the docs and I don't know how to make the tag field a hidden input. Perhaps a ModelForm isn't the way to go?

models.py:

class TagStatus(models.Model):
    user = models.ForeignKey(User, null=True, unique=True)
    status = models.CharField(max_length=2, choices=tag_statuses)
    tag = models.ForeignKey(Tag, null=True, blank=True)

    def __unicode__(self):
        return self.status

    def save(self, *args, **kwargs):
        super(TagStatus, self).save(*args, **kwargs)

class TagStatusForm(modelForm):
    class Meta:
        model = TagStatus
        fields = ('status','tag') 
        widgets = {
             'select': Select,
             'tag': ???
        }

django views.py:

@login_required
def tags(request):
    all_tags = Tag.objects.all()
    context = base_context(request)
    if request.method == 'POST':
        if 'status_check' in request.POST:
            status_form = TagStatusForm(request.POST)
            #if request.is_ajax():
            if status_form.is_valid():
                status_form.save()
                response = simplejson.dumps({"status": "Successfully changed status"})
            else:
                response = simplejson.dumps({"status": "Error"})
                return HttpResponse (response, mimetype='application/json')
    status_form = TagStatusForm()
    context['status_form'] = status_form
    context['all_tags'] = all_tags
    return render_to_response('tags/tags.html', context, context_instance=RequestContext(request))

template:

{% for tag in all_tags %}
....
<form class="nice" id="status-form" method="POST" action="">
     {% csrf_token %}
      <input type="hidden" name="status_check" />
      <input type='hidden' name="tag" value="{{ tag.name }}" />
     {{ status_form.status }}
</form>
...
{% endfor %}

How would I go about making a hidden input through a django ModelForm and then populate it through the template?

解决方案

To make a field in a ModelField a hidden field, use a HiddenInput widget. The ModelForm uses a sensible default widget for all the fields, you just need to override it when the object is constructed.

class TagStatusForm(forms.ModelForm):
    class Meta:
        model = TagStatus
        widgets = {'tag': forms.HiddenInput()}

这篇关于Django ModelForm有一个隐藏的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆