简体   繁体   English

将请求 POST 中的数据添加到表模板中 [Django, Python]

[英]Add data from request POST into a table template [Django, Python]

I create a table show book info from SQLite database and I'm trying to create an "Add New Book form" to make it add a new book into a table database.我从 SQLite 数据库创建了一个表显示书籍信息,我正在尝试创建一个“添加新书表单”以使其将新书添加到表数据库中。 How can I add a book and then make it show into the table Book Info?我怎样才能添加一本书,然后让它显示在表 Book Info 中?

<div class="container-fluid">
    <!-- Add Book -->
    <div class="add-book">
        <h1>Add a Book</h1>
        <form action="/add_book" method="POST" class="add_more_book">
            {% csrf_token %}
            <p>
                <label for="title" id="title">Title:</label>
                <input type="text" name="title" class="form-control" id="input-title" />
            </p>
            <p>
                <label id="desc" for="desc">Description:</label>
                <textarea class="form-control" name="description" id="input-desc" rows="3"></textarea>
            </p>
            <button type="submit" class="btn shadow btn-primary" id="addbtn">
                Add
            </button>
        </form>
    </div>
    <!-- End of Add Book -->

    <!-- Book info -->
    <div class="book-info">
        <form action="/" method="POST">
            {% csrf_token %}
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    {% for i in all_books_info %}
                    <tr>
                        <th scope="row">{{i.id}}</th>
                        <td>{{i.title}}</td>
                        <td><a href="/books/{{i.id}}">Views</a></td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </form>
    </div>

Lets say you have an a table named Bio in your models.py file.假设您的 models.py 文件中有一个名为 Bio 的表。 Like so像这样

    class Bio(models.Model):
        author = models.CharField(max_length= 20, null=True)
        book = models.CharField(max_length= 20, null=True)
        title = models.CharField(max_length=50)
        desc = models.CharField(max_length=200)

Assuming you have imported you models at the top of you views.py file.假设您已经在 views.py 文件的顶部导入了模型。

   from .models import *

The * imports all the models from the models file.( There are reasons you may or may not want to import all the models in the future.) * 从模型文件中导入所有模型。(您将来可能会或可能不想导入所有模型。)

In the views.py file在views.py 文件中

    def processInfo(request):
        print(request.POST["title"]
        if len(str(request.POST["title"])) < 2:
            print("Needs more the 2 characters to submit")
        else:
            print("Meets rule")
            Bio.objects.create(title = request.POST["title"])

The print is there to see if anything is received.打印件在那里查看是否收到任何东西。 You always want to check first if you are not getting an empty string.你总是想先检查你是否没有得到一个空字符串。 I use len and other tools to check https://www.w3schools.com/python/ref_func_len.asp There are a few ways on creating and getting data from the django models.我使用 len 和其他工具检查https://www.w3schools.com/python/ref_func_len.asp有几种方法可以从 django 模型中创建和获取数据。 https://docs.djangoproject.com/en/3.0/topics/db/queries/ https://docs.djangoproject.com/en/3.0/topics/db/queries/

Finally for the function that renders "/" in the views.py file.最后对于在views.py文件中呈现“/”的function。 Once you retrieve the data that you are looking for, return it with the request and html file.检索到要查找的数据后,将其与请求和 html 文件一起返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM