django在提交表单后没有更新我的数据库

xfyts7mz  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(246)

我已经创建了一个模型和一个表单,它们都正常工作,并且我已经使用管理模块向数据库添加了数据
models.py

class Client(models.Model):
    firstname = models.CharField(blank=True, max_length=30)
    lastname = models.CharField(blank=True, max_length=15)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, default="company")
    position = models.CharField(blank=True, max_length=15)
    country = CountryField(blank_label='(select country)')
    email = models.EmailField(blank=True, max_length=100, default="this_is@n_example.com")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    phone = PhoneField(default="(XX)-XXX-XXX")

    def __str__(self):
        return f'{self.firstname}'

forms.py

class ClientForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = ('firstname', 'lastname',"position",'country','email','phone')

views.py

@login_required
def add_client(request):
    if request.method == "POST":
        client_form = ClientForm(instance=request.user, data=request.POST)
        if client_form.is_valid():
            client_form.save()
            messages.success(request, 'You have successfully added a client')
        else:
            messages.success(request, "Error Updating your form")
    else:
        client_form = ClientForm(instance=request.user)

    return render(request,
                  "account/add_client.html",
                  {'client_form':client_form})

add_client.html

{% extends "base.html" %}

{% block title %}Client Information {% endblock %}

{% block content %}
  <h1> Client Form</h1>
  <p>Please use the form below to add a new client to the database:</p>
    <form method="post" enctype="multipart/form-data">
    {{ client_form.as_p }}
    {% csrf_token %}
    <p><input type="submit" value="Save changes"></p>
    </form>

{% endblock %}

一切似乎都很好,我可以在网站上提交数据,我收到一条消息说,提交时,罚款,但是,当我检查管理网站和检查数据库,我看不到新的数据添加,基本上是表单没有提交数据,我没有得到一个错误
[10/jul/2021 18:36:24]“get/account/add_client/http/1.1”200 12385[10/jul/2021 18:36:36]“post/account/add_client/http/1.1”200 12616
有人知道我做错了什么吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题