【问题标题】:how to transfer values from html form to Django admin?如何将值从 html 表单传输到 Django 管理员?
【发布时间】:2016-02-01 17:07:22
【问题描述】:

我制作了一个 html 表单,其中自动建议选项也在城市字段中。我想知道如何将html表单的值传递给django admin。

到目前为止,我制作了一个表单模型,其中姓名、城市、电话号码、性别等字段在 admin.py 中注册。通过这个我无法直接由 django 管理员注册。

【问题讨论】:

  • 我不完全确定你在问什么?您是否想知道是否可以在 django 管理面板中注册一个 HTML 表单?

标签: python html django django-models django-forms


【解决方案1】:

如果您已经有了 html 表单,则必须在 views.py 中创建函数。 在这个文件中,您必须编写代码来接收您从 html 表单发送的数据。

阅读文档:Django forms

你可能有一个像下面这样的 html 表单:

<form action="/your-name/" method="post">
    <label for="your_name">Your name: </label>
    <input id="your_name" type="text" name="your_name" value="{{ current_name }}">
    <input type="submit" value="OK">
</form>

您可以在 forms.py 中创建一个表单:

from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)

您的函数可能如下所示(在 views.py 中)

from django.shortcuts import render
from django.http import HttpResponseRedirect

from .forms import NameForm

def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required
        # ...
        # redirect to a new URL:
        return HttpResponseRedirect('/thanks/')

# if a GET (or any other method) we'll create a blank form
else:
    form = NameForm()

return render(request, 'name.html', {'form': form})

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2022-08-15
    • 2019-04-25
    • 2019-08-27
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多