从Django表格中恢复数据

发布于 2025-02-02 03:14:13 字数 1821 浏览 1 评论 0原文

这是我第一次进行Django形式挑战。 目前,挑战仅来自HTML模板获取 last_name 信息,然后从视图中存储到db中。因此,我有以下方案:

models.py:

class Person(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)

一个html模板,我将仅放置在表单部分下方:

      <form action="{% url 'store' %}" method="post">
        {% csrf_token %}
        <div class="form-group row">
          <label for="last_name" class="col-sm-2 col-form-label">Last Name</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="last_name" placeholder="Last Name">
          </div>
        </div>

        <div class="form-group row">
          <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Send Last Name</button>
          </div>
        </div>
      </form>

and views.py本身...我创建了一个insertlastname modelform来存储表单中的数据,但没有工作。由于某种原因,当我尝试调用form.cleaned_data时,我重新发现了有关IS_VALID()验证的错误。像这样,我没有从标签中获取信息。

class InsertLastName(forms.ModelForm):
    class Meta:
        model = Person

        fields = ['last_name']
        exclude = ['first_name']

def index(request):
    persons = Person.objects.all()
    context = {'persons': persons}
    return render(request, '/index.html', context)

def store(request):
    form = InsertLastName(request.POST or None)
    print(form.cleaned_data['last_name'])

    return HttpResponse("Storing a new Last Name object into storage")

从我的表单中获取信息的正确方法是什么? 我正在关注 documentation < /a>并从挑战模板进行编码。

It's my first time doing a Django Form challenge.
The challenge for now is just from a HTML Template get the last_name information and store into DB from views. So I have a scenario below:

models.py:

class Person(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)

an HTML template, I will put below only the form part:

      <form action="{% url 'store' %}" method="post">
        {% csrf_token %}
        <div class="form-group row">
          <label for="last_name" class="col-sm-2 col-form-label">Last Name</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="last_name" placeholder="Last Name">
          </div>
        </div>

        <div class="form-group row">
          <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Send Last Name</button>
          </div>
        </div>
      </form>

And the views.py itself... I created a InsertLastName ModelForm to store the data from the form, but didn't work. For some reason, when I tryed to call form.cleaned_data I reveived an error about the is_valid() verification. Seens like that I'm not getting the information from label.

class InsertLastName(forms.ModelForm):
    class Meta:
        model = Person

        fields = ['last_name']
        exclude = ['first_name']

def index(request):
    persons = Person.objects.all()
    context = {'persons': persons}
    return render(request, '/index.html', context)

def store(request):
    form = InsertLastName(request.POST or None)
    print(form.cleaned_data['last_name'])

    return HttpResponse("Storing a new Last Name object into storage")

What is the correct way to get the information from last_name label in my form?
I'm following that documentation and coding from a challenge template.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2025-02-09 03:14:13

您的输入html标签的仅设置atter名称
示例:

<input type="text" name="first">

并且要在view.py中获取输入在函数存储中:

request.POST.get("first")

将其添加到您的html模板中

{{persons}}

Your just set attr name for your input html tag
Example:

<input type="text" name="first">

And for get input in function store in view.py:

request.POST.get("first")

Add this to your html template in your form

{{persons}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文