【发布时间】:2018-10-08 18:40:34
【问题描述】:
我正在尝试实现一个功能,当我单击激活按钮时,我的 Django 数据库中该字段的值更改为“活动”并在 HTML 页面上显示为活动。如果点击 deactivate,文本会随着它在 django 数据库中的值更改为 deactive。
这是我当前的 HTML 页面:
<div class="col-sm-5">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Job activation status</h3>
</div>
<div class="panel-body">
<b>Status</b>: Active
<span style="padding-left:20px;"></span>
<button type="button" class="btn btn-primary">Activate</button>
<button type="button" class="btn btn-primary">Deactivate</button>
<button type="button" class="btn btn-primary">Dissolve</button>
</div>
</div>
</div>
而我的models.py如下:
class Job(models.Model):
def __unicode__(self):
return self.name
name = models.CharField('Job Name',max_length=128)
# when the job was created
date_created = models.DateTimeField('Date Created', auto_now=True)
# what entity/organization needs this job?
client_organization = models.CharField('What organization do you represent?', max_length=64)
# short description
description = models.TextField('Job Description', max_length=256)
# end product to be delivered
deliverable = models.TextField('Deliverable', max_length=256)
# when Job is due for completion
duedate = models.DateTimeField('Date Due')
# all persons who may be affected by project
#stakeholders = models.TextField('Stakeholders')
# important technical requirements
#additional_information = models.TextField('Additional Information', blank = True)
# budget estimate
#budget = models.CharField('Budget', max_length=64)
# file attachments
#attachments = models.FileField(upload_to='job', blank = True)
creator = models.ForeignKey(User,related_name = 'jobs')
organizations = models.ManyToManyField(Organization, through = 'JobRequest', blank=False, null=True)
#organizations = models.CharField(default="nothing",null=True,max_length = 256)
contact_information = models.CharField('Contact Information', max_length = 256, blank = False, null=True)
skill_required = models.CharField('Volunteer skills required', max_length=256, blank = False, null=True)
hours_day = models.CharField('Number of hours per day', max_length=256, blank = False, null=True)
# Job is closed after a jr is confirmed
closed = models.BooleanField(default = False)
# some tags to determine what organizations to submit job to
categories = models.ManyToManyField(Category, related_name = 'jobs')
#categories = models.CharField(default="nothing",null=True, max_length = 256)
status = models.IntegerField(default = 0, choices = ((0, 'Pending'), (1, 'Approved'), (2, 'Disapproved'), (3, 'Closed')))
class Meta:
permissions = (
( 'view_job','Can view Job' ),
( 'edit_job','Can edit Job'),
( 'is_creator', 'Is a creator of Job')
)
我需要帮助来向 models.py 添加一个字段,该字段会根据 HTML 中的按钮单击而变化,并根据单击的按钮在 HTML 页面中显示活动/非活动状态。我还需要帮助来相应地修改 HTML 页面
【问题讨论】:
-
如果您尝试在数据库中进行此更改而不重新加载页面,那么您需要调用 ajax 函数来更新数据库中的此值。如果不是你的情况,那么你只需要做一个标准的表单提交。
-
我确实想在更改数据库时重新加载页面。
-
我在编写代码时需要帮助。
-
在您的 django 代码中编写一个视图,该视图将更改您的数据库中的值,然后在用户触发按钮 [active,inactive] 时使用 js (ajax) 调用该视图