问题描述
在我的 Django 应用程序中,我想要一个小睡按钮,我想使用它来推迟对象的截止日期。相应的模型有一个名为 alarm 的日期字段,我想将 snooze 命令与应添加到当前设置的 x 天数一起传递。到目前为止,我发现我无法直接从模板中执行此操作,并且多个站点建议使用 Ajax。我从来没有用过。
我的文件看起来像这样。 views.py
def Operation_Snooze_Method(request):
print(request)
if request.POST:
if request.is_ajax():
print("Ajax")
# I guess here I would handle the alarm if I could bring the Ajax call to work at all.
else:
print("no ajax")
print(request.POST)
return render(request,templ_folder_name + landingpage)
我的models.py
的一部分class Operation(models.Model):
title = models.CharField(max_length=255,blank=False,null=False,verbose_name="Title of the operation")
alarm = models.DateField(verbose_name="Date of the alarm",null=True,blank=True)
[...]
def Operation_Snooze(self,x):
self.alarm = datetime.now() + timedelta(days=x)
urls.py
path('snooze/',views.Vorgang_Snooze_Method,name="snooze"),
我的base.html,这是我用其他模板扩展的模板
$document.ready(function(){
$("#snooze").submit(function(event){
event.preventDefault();
$ajax({
type:"POST",url:"{% url 'app_name_myproject:snooze' %}",data:{
'id':$('#snooze').val()
'csrfmiddlewaretoken':$("input[name=csrfmiddlewaretoken]").val()
},});
return false;
};)
});
</script>
[...]
{% if urgent_operations %} # this is a list of operations,just strings as a list
<div class="col-sm-6 col-sm-offset-3 col-md-9 col-md-offset-2 main">
<div style="border:solid; border-color:red; padding:10px;">
<strong>Urgent operations:</strong><br>
<table>
{% for uo in urgent_operations %}
<tr>
<td style="padding-right:30px;">{{dv}}<td>
<td><form method="post" id="snooze" action="">{%csrf_token%}<button class="btn btn-block btn-default" name="id" value="13" type="submit">Snooze for 7 days</button></form></td> # the 13 has no meaning. Later I want to add the id of the corresponding object,but this is just for testing purposes.
</tr>
{% endfor %}
</table>
</div>
</div>
{% endif %}
由于我还没有完成所有工作,很明显贪睡功能无法工作,但我什至在 Ajax 部分遇到了问题,我必须首先找出原因。当我单击按钮时,整个登录页面会重新加载。我认为使用 Ajax 可以以某种方式仅重新加载页面的一部分或触发服务器上的功能。因此,我想说一下,我只需单击按钮即可触发闹钟贪睡功能。如果可能的话,我只会重新加载模板的那部分,以检查是否有紧急操作即将到来。
PS:我希望我的英语没问题。也许操作是一种误导性的翻译。它是关于任务或任何你想称呼它们的。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)