gpt4 book ai didi

python - 如何在 HTML 中单击按钮时更改 django 中的值?

转载 作者:行者123 更新时间:2023-12-05 08:54:25 26 4
gpt4 key购买 nike

我正在尝试实现一个功能,在该功能中,当我单击“激活”按钮时,我的 Django 数据库中此字段的值更改为“事件”并在 HTML 页面上显示为事件状态。如果单击停用,文本将更改为停用以及它在 django 数据库中的值。

这是我当前的 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 页面

最佳答案

如果您只需要两种状态(事件和非事件),您需要一个 BooleanField,因此在您的模型中添加如下内容:

active = models.BooleanField(default = False)

然后,您需要在 views.py 中创建一个函数来更新数据库中的模型(我将其命名为 ajax_change_status):

from django.http import JsonResponse
from xxx.models import Job

def ajax_change_status(request):
active = request.GET.get('active', False)
job_id = request.GET.get('job_id', False)
# first you get your Job model
job = Job.objects.get(pk=job_id)
try:
job.active = active
job.save()
return JsonResponse({"success": True})
except Exception as e:
return JsonResponse({"success": False})
return JsonResponse(data)

然后,您在 urls.py 中为您的 ajax 函数添加 url:

url(r'^ajax/change_status/$', views.ajax_change_status, name='ajax_change_status')

最后在您的 HTML 中,您需要在每次单击按钮时调用该函数:

<script>
$("#button_id").on('click', function () {
var username = $(this).val();
var active = <true> // or false, you have to set it
var active = <id> // you have to set it
$.ajax({
url: '/ajax/validate_username/',
data: {
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
'active': active
'job_id': username
},
dataType: 'json',
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});

});
</script>

最后,不要忘记将 csrf 标签放入您的 HTML 中:

{% csrf_token %}

关于python - 如何在 HTML 中单击按钮时更改 django 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com