如果form.is_valid()与来自views.py的变量一起运行JavaScript

时间:2019-02-26 10:18:42

标签: javascript python django

我正在尝试使用javaScript将变量打印到Dymo打印机。我已经测试了javaScript代码,它可以工作,但是我不知道如何从我的views.py访问和运行此javaScript。也许我可以从模板执行此操作,但是我尝试打印的变量包含一些在表单有效时创建的数据库信息。

请询问是否不清楚!谢谢!

Views.py

def CreateWasteView(request):
  if request.method == 'POST':
    form = CreateWasteForm(request.POST)
    if form.is_valid():
        waste = Waste_Item(material=form['materialList'].value(), height=form['height'].value(), width=form['height'].value())
        waste.save()
        waste.code = form['materialList'].value() + str(waste.pk)
        waste.save()
    return render(request, 'waste/home.html')
    else:
        form = CreateWasteForm()
        context = {
            'form': form
        }
        return render(request, 'waste/new.html', context)

模板

{% extends "waste/base.html" %}
{% load crispy_forms_tags %}
{% block content %}

<div class="content-section">
    <form method="POST">
        {% csrf_token %}
        <div class="col-md-6">
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">New Waste-Item!</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Create</button>
            </div>
        </div>
    </form>
</div>
{% endblock content %}

JavaScript

<script type="text/javascript" 
src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.3.0.js"></script>
<script type="text/javascript">
function startupCode(code) {
  dymo.label.framework.trace = 1;
  var printers = dymo.label.framework.getPrinters();
  if (printers.length == 0)
    throw "No DYMO printers are installed. Install DYMO printers.";

  dymo.label.framework.getPrintersAsync().then(function(printers){
    // Successful result, printers variable has a list of all supported by the DYMO Label Framework
console.log(printers);
  }).thenCatch(function(error){
// Error
  });

   var labelXml = '<?xml version="1.0" encoding="utf-8"?>\
                    <DieCutLabel Version="8.0" Units="twips">\
                        <PaperOrientation>Landscape</PaperOrientation>\
                        <Id>Address</Id>\
                        <PaperName>30252 Address</PaperName>\
                        <DrawCommands>\
                            <RoundRectangle X="0" Y="0" Width="1440" Height="1440" Rx="180" Ry="180" />\
                        </DrawCommands>\
                        <ObjectInfo>\
                        <TextObject>\
                            <Name>TEXT1</Name>\
                            <ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
                            <BackColor Alpha="0" Red="255" Green="255" Blue="255" />\
                            <LinkedObjectName></LinkedObjectName>\
                            <Rotation>Rotation0</Rotation>\
                            <IsMirrored>False</IsMirrored>\
                            <IsVariable>False</IsVariable>\
                            <HorizontalAlignment>Center</HorizontalAlignment>\
                            <VerticalAlignment>Top</VerticalAlignment>\
                            <TextFitMode>AlwaysFit</TextFitMode>\
                            <UseFullFontHeight>False</UseFullFontHeight>\
                            <Verticalized>False</Verticalized>\
                            <StyledText>\
                                <Element>\
                                    <String>BARCODE</String>\
                                    <Attributes>\
                                    <Font Family="Arial" Size="12" Bold="False" Italic="False"\
                                    Underline="False" Strikeout="False" />\
                                    <ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
                                    </Attributes>\
                                </Element>\
                            </StyledText>\
                        </TextObject>\
                         <Bounds X="336" Y="132.900024414063" Width="2265" Height="345" />\
                        </ObjectInfo>\
                    </DieCutLabel>';

  var label = dymo.label.framework.openLabelXml(labelXml);
  label.setObjectText("TEXT1", code);
  label.print("DYMO LabelWriter 450 Turbo"); // This is the NAME of the 
printer which i found 
}
</script>

1 个答案:

答案 0 :(得分:0)

您可以为此目的通过视图将所需变量传递给javascript。示例:

views.py

if form.is_valid():
    waste = Waste_Item(material=form['materialList'].value(), height=form['height'].value(), width=form['height'].value())
    waste.save()
    waste.code = form['materialList'].value() + str(waste.pk)
    waste.save()
    value = '' #the value you want to pass
return render(request, 'waste/home.html', {'value':value})

然后,您可以在模板中使用{{ value }}获取值。然后,您可以将此值传递给要调用的函数。