在django中提交时设置一个布尔值

时间:2017-05-05 14:45:55

标签: python django django-forms django-templates django-views

我想在提交重新测试表单时将重新测试模型中的布尔字段is_rep设置为true。 现在它只是在重新测试模型中加起来。 因为我想在提交请求时触发其他事件。

我的代码

models.py

class Retest(models.Model):

semester = models.ForeignKey(Semester)
dept = models.ForeignKey(Departement)
batch = models.ForeignKey(Batch)
date = models.DateField(default=0)
subject = models.ForeignKey(Subject)
name = models.CharField(max_length=50)
admnno = models.CharField(max_length=50)
reason = models.CharField(max_length=50)
proof = models.CharField(max_length=200)
is_hod = models.BooleanField(default=False)
is_principal = models.BooleanField(default=False)
notify = models.BooleanField(default=False)
is_sure = models.BooleanField(default=False)
is_rep = models.BooleanField(default=False)

def get_absolute_url(self):
    return reverse( 'retest:retestform')
def __str__(self):
             return self.name

urls.py

url(r'^retest/retestform/$',login_required(views.RetestCreate.as_view()), name='retestform')

views.py

class RetestCreate(CreateView):
model = Retest
fields = ['semester', 'dept', 'batch', 'date', 'subject', 'name', 'admnno', 'reason', 'proof', 'is_sure']

模板

<form class="form_horizontal" action="" method="post" enctype="multipart/form-data" >
        {% csrf_token %}
        {% include 'retest/form-template.html' %}
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-success">Submit</button>

          </div>
        </div>
      </form>

1 个答案:

答案 0 :(得分:0)

form_template

{% for field in form %}

    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <span class="text-danger small">{{ field.errors }} </span>
    </div>
    <label class="control-label col-sm-2">{{ field.label_tag }} </label>
    <div class ="col-sm-12">
    <div class="form-control">
    {{ field }}</div></div>
    </div>


{% endfor %}