【问题标题】:Handling Form to submit API call and display API result in the same page处理表单提交API调用并在同一页面显示API结果
【发布时间】:2021-03-14 16:14:46
【问题描述】:

我正在使用表单来获取数据,并从该数据中,我将使用特定的解决方案 API 来使用此表单中的信息创建一个站点。

我想从这个表单中获取数据,使用 API 创建站点,但我想获取 API 结果数据并将内容显示在同一表单页面但在提交底部下方。

我能够创建基本表单、获取必要的数据并使用 API 调用创建站点,但我不知道我必须做什么才能获取 API 结果并在成功时显示它。 ``

class SiteForm(forms.Form):
    site_name = forms.CharField(max_length=100, label="Site Name",
                                widget=forms.TextInput(attrs={'placeholder': 'Demo API'}))
    country = forms.CharField(label="Country",
                              widget=forms.TextInput(attrs={'placeholder': 'Country',}))
    time_zone = forms.CharField(max_length=100, label="Timezone",
                                widget=forms.TextInput(attrs={'placeholder': 'City'}))

    location = forms.CharField(max_length=200, label="Location",
                               widget=forms.TextInput(
                                   attrs={'placeholder': 'Street'}))

我不知道这是否是最好的代码,但至少 POST 工作正常,我可以使用 API 调用创建我的网站。 我的意见.py

class SiteCreateView(FormView):
    template_name = 'site_config.html'
    form_class = SiteForm

    def get(self, request, *args, **kwargs):

        return render(request)

    def post(self, request, *args, **kwargs):

        site_info = {}
        site_info['name'] = request.POST.get("site_name")
        site_info['timezone'] = request.POST.get("country")
        site_info['country_code'] = request.POST.get("time_zone")
        site_info['address'] = request.POST.get("location")

        data_post = json.dumps(site_info)
        token = '<token-id>'
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Token ' + token
        }
        url = '<API URL>'

        response = requests.post(url, data=data_post, headers=headers)

        new_site_info = json.loads(response.content.decode('utf-8'))

        return render(self.request, self.template_name, self.get_context_data())

这是我的基本 .html

{% extends 'site_base.html'%}
{% block title %}Site Config{% endblock title%}
{% block content %}
    <h1>Create Site</h1>
    <form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input class="btn btn-success" type="submit">
    </form>
{% endblock content %}

【问题讨论】:

  • api url 是你自己的应用还是第三方的东西?

标签: django django-forms


【解决方案1】:

我不使用基于类的视图,但我怀疑您可以执行以下操作:

response = requests.post(url, data=data_post, headers=headers)
# get the normal context
context = self.get_context_data()
# add the results from the api
context['new_site'] = json.loads(response.content.decode('utf-8'))
# hand off the normal with a new key for the new_site data
return render(self.request, self.template_name, context)

然后在您的模板中类似于:

{% block content %}
    <h1>Create Site</h1>
    <form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input class="btn btn-success" type="submit">
    </form>
    {% if new_site %}
       {{ new_site.some_attribute }}
    {% endif %}
{% endblock content %}

【讨论】:

    【解决方案2】:

    谢谢!它有效,但我还有一个额外的问题,我在这里很难理解。 为什么您的代码将结果重定向到同一页面而下面的这个没有?

    new_site = response.json()
    return render(request, self.template_name, {
        'name': new_site
    })
    

    【讨论】:

    • 我怀疑是self.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2018-10-07
    • 2021-11-13
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多