我需要通过 Python/Django 中的 html 提交按钮将数据更新到数据库(mysql)的代码。需要带有 html 代码的 view.py 内容。考虑我有一个带有书名、作者名和出版商名的表我需要更新这些值的值通过 html 提交按钮。
请在这方面帮助我..我是这个框架的新手...无法通过。
如果您向我发送通过 Django/Python 在 mysql 中编辑表的代码,那将更加感谢
请在不使用表单的情况下为我提供代码,我使用的是 html 模板而不是表单...请
如果你的html表单是这样的
<form method="post" action=".">
<input type="text" id="field1" name="field1 />
<input type="text" id="field2" name="field2 />
</form>
您可以像这样在views.py中获取值:
if request.POST:
field1_value = request.POST['field1']
field2_value = request.POST['field2']
模型.py
class Book(models.Model):
book_name = models.CharField(max_length = 32, unique = True)
author_name = models.CharField(max_length = 32, unique = True)
publisher_name = models.CharField(max_length = 32, unique = True
)
视图.py
def save_book(request):
if request.POST:
book_name = request.GET.get('book_name')
author_name = request.GET.get('author_name')
publisher_name = request.GET.get('publisher_name')
Book.object.creat(book_name, author_name,publisher_name)
在 html 中,使用查询字符串传递这些值