存档十一月 2019

visual studio 调试/运行加载 microsoft调试符号缓慢、卡屏

表现在,无论是debug还是release模式,运行程序时,visual studio 2019左下角,显示正在加载xxx.dll(名字随机,譬如picface.dll,kernelbase.dll,ntdll.dll ),输出屏幕无显示,只有光标闪烁,直至出对话框:

64位调试操作花费的时间比预期要长。

点击“停止调试”按钮,visual studio 界面会发生无响应。

进入菜单:工具—>选项—>调试,不勾选Microsoft符号服务器,可以保留NuGet.org符号服务器(比微软符号服务器快一点)

微软不再提供脱机符号,它建议使用 https://msdl.microsoft.com/download/symbols ,但是依旧很慢。也取决于你宽带的ISP,不同区域的电信宽带,国外网站访问速度都有区别。

符号加载速度正常时,调试的开始、停止,秒响应。

How to disable input history in Django forms?

禁用在django生成的表单中,input字段的输入历史。

You can disable autocomplete on such fields by setting the HTML5 autocomplete attribute to off. To achieve this in Django’s forms you need to pass additional information to the widget for each input, e.g.:

class MyForm(forms.Form):

    secret = forms.CharField(widget=forms.TextInput(attrs={'autocomplete': 'off'}))

使用tempus_dominus日期插件时

failure_start = forms.DateTimeField(label='维修开始',
                                        widget=DateTimePicker(
                                            options={
                                                "format": "YYYY-MM-DD HH:mm",
                                            },
                                            attrs={'icon_toggle': True, 'autocomplete': 'off'}))

搭建自己的Blynk server

在proxmox 5.4里,安装虚拟机 openwrt, 虚拟机ubuntu,整合利用资源。

因为Blynk server使用java,所以ubuntu主机需要安装java

sudo apt update
sudo apt install openjdk-8-jdk openjdk-8-jre
java -version 验证安装

在github blynkkk/blynk-server下载server-0.41.11.jar

保存在用户目录即可。

建立data文件夹,使用命令验证,或者创建systemd服务

java -jar /home/yu/server-0.41.11.jar -dataFolder /home/yu/data

建立文件:

/etc/systemd/system/cat blynk.service

[Unit]
Description=blynk server service

[Service]
ExecStart=/usr/bin/java -jar /home/yu/server-0.41.11.jar -dataFolder /home/yu/data &
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

使用whereis java,得到java的完整路径。

yu@y:/etc/systemd/system$ systemctl status blynk
● blynk.service - blynk server service
   Loaded: loaded (/etc/systemd/system/blynk.service; enabled; vendor preset: enabled)
   Active: activating (start) since Sun 2019-11-24 11:09:16 UTC; 3h 15min ago
 Main PID: 801 (java)
    Tasks: 27 (limit: 4660)
   CGroup: /system.slice/blynk.service
           └─801 /usr/bin/java -jar /home/yu/server-0.41.11.jar -dataFolder /home/yu/data &

Nov 24 11:09:16 y systemd[1]: Starting blynk server service...
Nov 24 11:09:37 y java[801]: Blynk Server  successfully started.
Nov 24 11:09:37 y java[801]: All server output is stored in folder '/logs' file.
yu@y:/etc/systemd/system$ cat /logs/blynk.log
11:09:30.943 INFO - Using data dir '/home/yu/data'
11:09:32.090 INFO - Region : local. Host : 127.0.1.1.
11:09:32.195 INFO - Using native epoll transport.
11:09:33.758 INFO - Initializing gmail smtp mail transport. Username : example@gmail.com. SMTP host : smtp.gmail.com:587
11:09:33.914 INFO - Reports : 0
11:09:33.917 INFO - Didn't find custom user certificates.
11:09:33.919 INFO - Didn't find Let's Encrypt certificates.
11:09:33.919 WARN - You didn't specified 'server.host' or 'contact.email' properties in server.properties file. Automatic certificate generation is turned off. Please specify above properties for automatic certificates retrieval.
11:09:33.919 INFO - Using native openSSL provider.
11:09:33.919 WARN - ATTENTION. Server certificate paths (cert : '/', key : '/') not valid. Using embedded server certs and one way ssl. This is not secure. Please replace it with your own certs.
11:09:37.098 INFO - HTTP API and WebSockets server listening at 8080 port.
11:09:37.100 INFO - HTTPS API, WebSockets and Admin page server listening at 9443 port.

systemctl status blynk,查看服务运行状态。

在openwrt中开启3个端口。

端口在blynk.log中显示,可以修改。

blynk私服,默认电池是10万,足够我们使用了。

django: ajax upload file through form input

通过表单提交一个txt文本文件到pyton后台,使用ajax函数无刷新提交。

加热棒编织导线破损更换
可控硅,保险更换
断路器及接触器
推料器故障检修
加压异常(比例阀)故障
台车故障检修
液压泵检修
油压电磁阀检修
油压管道检修
传送带检查
侧模感应器检查检修
主油缸故障
上模油缸检查、修理
上模感应器故障

前端html

form部分

<form id="form_import">
        {% csrf_token %}
        <div class="row">
            <div class="col-6">
                <label for="id_procedure">工序</label>
                <select name="procedure" id="id_procedure" class="form-control">
                    {% for p in procedure %}
                        <option value="{{ p.0 }}">{{ p.1 }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="row">
            <div class="col-3">
                <label for="id_file_imported">选择保护故障类型的txt文件</label>
                <input name="m_file" type="file" id="id_file_imported" class="form-control-file"/>
            </div>
            <div class="col-3">
                <button type="submit" id="btn_import" class="btn btn-primary">导入</button>
            </div>
            <input type="text" name="m_method" value="import" hidden/>
        </div>
    </form>

ajax部分

 $("#form_import").submit(function (event) {
                event.preventDefault();
                let data = new FormData(this);
                $.ajax({
                    url: "{% url 'simple_enroll:failure_type' %}",
                    type: 'POST',
                    dataType: 'JSON',
                    processData: false,  //不让jquery处理我的数据
                    contentType: false,
                    data: data,
                    success: function (data) {
                        alert(data);
                    }
                });
            });

注意 processData,contentType选项要关闭。

后台view

def failure_type_view(request):
    if request.method == 'POST':
        m_method = request.POST['m_method']
        if m_method == 'import':
            file = UploadedFile(request.FILES['m_file'])
            f = BytesIO()
            for chunk in file.chunks():
                f.write(chunk)
            str_f = f.getvalue().decode('utf-8').split('\t\r\n')
            failure_type = request.POST['procedure']
            import_failure_type(file, failure_type)
    else:
        return render(request, 'simple_enroll/failure_type.html', {'procedure': PROCEDURE_CHOICES})

文件小于2.5M时,django将上传的内容放在内存中,保存格式InMemoryUploadedFile。

Where uploaded data is stored
Before you save uploaded files, the data needs to be stored somewhere.

By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold the entire contents of the upload in memory. This means that saving the file involves only a read from memory and a write to disk and thus is very fast.

However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system’s temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload. If an upload is large enough, you can watch this file grow in size as Django streams the data onto disk.

These specifics – 2.5 megabytes; /tmp; etc. – are simply “reasonable defaults” which can be customized as described in the next section.

chunk读出的内容是bytes字节,需要decode后生成字符串。直接链接做split,得到字符串列表。

django 数据表筛选符合时间范围的记录

前端时间选择,使用插件 tempusdominus-bootstrap4

页面ajax提交表单数据

$("#btn_query").click(function () {
                $.ajax({
                    url: '{% url 'enroll:simple_query' %}',
                    type: 'POST',
                    dataType: 'JSON',
                    data: $('#form_query').serialize(),
                    success: function (arg) {
                        if (arg.status === true) {
                            console.log("成功提交");
                        } else {
                            console.log(arg.message);
                            let alert4 = $("#modal-alert-4");
                            alert4.html(arg.message);
                            alert4.iziModal('open', this);

                        }
                    }
                });

            });

view视图

# 查询
def simple_query_view(request):
    if request.method == 'POST':
        factory = request.POST['factory']
        query_start = datetime.strptime(request.POST['query_start'], '%Y-%m-%d %H')
        query_end = datetime.strptime(request.POST['query_end'], '%Y-%m-%d %H')
        # is_group = request.POST['is_group']
        # is_descend = request.POST['is_descend']

        q1 = RollInRecord.objects.filter(failure_time__range=[query_start, query_end])
        data = []

    else:
        return render(request, 'enroll/simple_query.html', {'procedure': PROCEDURE_CHOICES})

failure_time是数据库字段,附加__range后缀,求俩时间之间的范围。

django settings.py static files

在project的settings.py,末尾设置static的值:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

BASE_DIR 值为当前project的路径。

STATICFILES_DIRS 是一个列表。

开发环境,在html文件中硬代码/static/jquery.js,不需要使用修改urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# 不需要此修改

Django, Passing a 3-tuple or anapp_nametoinclude()

Django中的url反向解析

site url 站点URL

from django.contrib import admin
from django.urls import path, include

import enroll

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'roll/', include('enroll.urls')),
]

app url 应用URL

from django.urls import path

from . import views


app_name = 'enroll'
urlpatterns = [
    path('', views.index_view, name='index'),
    path('person', views.person_view, name='person'),
    path('simple_query', views.simple_query_view, name='simple_query'),
]

template使用

<ul class="nav nav-pills">
    <li class="nav-item">
        <a class="nav-link active" href="{% url 'enroll:index' %}">录入保修单</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="{% url 'enroll:simple_query' %}">简单报表</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#">周报表</a>
    </li>
    <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
    </li>
</ul>

Error: You need to install postgresql-server-dev-NN for building a server-side extension or libpq -dev for building a client-side application.

pip install psycopg2 ,发生错误。

Downloading https://files.pythonhosted.org/packages/84/d7/6a93c99b5ba4d4d22daa3928b983cec66df4536ca                                    50b22ce5dcac65e4e71/psycopg2-2.8.4.tar.gz (377kB)
     |████████████████████████████████| 378kB 367kB/s
    ERROR: Command errored out with exit status 1:
     command: /home/a/m_repair/env/bin/python2 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"                                    '"'/tmp/pip-install-_VNrPa/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-_VNrPa/psycopg2/set                                    up.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"',                                     '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-i                                    nstall-_VNrPa/psycopg2/pip-egg-info
         cwd: /tmp/pip-install-_VNrPa/psycopg2/
    Complete output (8 lines):
    running egg_info
    creating /tmp/pip-install-_VNrPa/psycopg2/pip-egg-info/psycopg2.egg-info
    writing /tmp/pip-install-_VNrPa/psycopg2/pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing top-level names to /tmp/pip-install-_VNrPa/psycopg2/pip-egg-info/psycopg2.egg-info/top_le                                    vel.txt
    writing dependency_links to /tmp/pip-install-_VNrPa/psycopg2/pip-egg-info/psycopg2.egg-info/depen                                    dency_links.txt
    writing manifest file '/tmp/pip-install-_VNrPa/psycopg2/pip-egg-info/psycopg2.egg-info/SOURCES.tx                                    t'
    Error: You need to install postgresql-server-dev-NN for building a server-side extension or libpq                                    -dev for building a client-side application.

根据提示,安装libpq即可。

sudo apt install libpq-dev

电影推荐

Korea film: 여자 하숙집 3 2019

django:Field ‘id’ doesn’t have a default value

数据表

class Person(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=20, verbose_name='电话')
class Record(models.Model):    
    repairer = models.ManyToManyField(Person, '维修者')

表单

class RollForm(Form):
    repairer = forms.ModelMultipleChoiceField(Person.objects.filter(is_valid='True'), label='维修者',
                                              widget=forms.SelectMultiple(attrs={'class': 'form-control'}))

视图

def index_view(request):
     if request.method == 'POST':
         m_form = RollForm(request.POST)
         result = {}
         if m_form.is_valid():
             result['status'] = True
             # save
             record =Record()
             record.save()  # 先保存
             record.repairer.set(m_form.cleaned_data['repairer'])  # 多对多需要1个id

对于多对多字段,保存到数据库时,先save,而后再添加多对多字段的值。django官方文档

Create an Article:

>>> a1 = Article(headline='Django lets you build Web apps easily')

You can’t associate it with a Publication until it’s been saved:

>>> a1.publications.add(p1)
Traceback (most recent call last):
...
ValueError: "<Article: Django lets you build Web apps easily>" needs to have a value for field "id" before this many-to-many relationship can be used.

Save it!

>>> a1.save()

Associate the Article with a Publication:

>>> a1.publications.add(p1)

电影推荐

Korea film: 여자 하숙집 3 2019