如何使用Django使用“ objects.create”将数据添加到mysql表中

时间:2019-05-19 20:05:30

标签: python mysql django django-models

我正在建立一个新网站,并想将数据添加到mysql表中, 使用“ objects.create”,让用户输入数据文本,然后创建到数据库中。

像这样

模型

class Record(models.Model):
    record_id = models.AutoField(primary_key=True)
    compiler_name = models.CharField(max_length=45)
    store = models.CharField(max_length=45)
    goods = models.CharField(max_length=45)
    account = models.CharField(max_length=45)
    number = models.CharField(max_length=45)

    class Meta:
        managed = False
        db_table = 'record'


观看次数

def tech2(request):

    status_a = True

    status_check_account = True

    if request.method == 'POST':
        check_account = request.POST.get('account', '')
        check_store = request.POST.get('store', '')

        check_A = Record.objects.filter(account__exact=check_account)
        check_S = Record.objects.filter(store__exact=check_store)

        if check_A:  # 如果回傳陣列是空的
            status_a = False


        R1 = request.POST.get('account', '')
        R2 = request.POST.get('account_check', '')

        if R1 == R2:  # !=改成is not
            status_check_account = True
        else:
            status_check_account = False

        accounts = request.POST.get('account', '')
        stores = request.POST.get('store', '')
        compiler_names = request.POST.get('compiler_name ', '')
        good = request.POST.get('goods', '')
        numbers = request.POST.get('number', '')
        if status_a is True and status_check_account is True:

            Record.objects.create(goods=good, compiler_name=compiler_names, number=numbers, account=account,, store=stores)
            return HttpResponseRedirect('/tech/')



我想建立一个销售网站让我们使用类型数据

tech.html



            <p class="float">
                <label for="tech">&nbsp;B</label>
                <input type="text" name="compiler_name" required autofocus value={{ compiler_name }}>

            </p>            

0 个答案:

没有答案