簡體   English   中英

如何在Django模板中將API與HTML集成?

[英]how to integrate API with HTML in django template?

我通過python請求同時使用POST和GET方法來獲取數據並在API中提交數據。

class ApiLoginView(TemplateView):
    template_name = 'index.html'

    def post(self,request):
        email = request.POST.get('email')
        print(email)

        password = request.POST.get('password')
        print(password)
        API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxx'
        API_URL = 'http://dev.com/rest/storeLogin'
        parameter = {
            'authToken':API_KEY,
            'email':email,
            'password':password,
        }
        r = session.post(url = API_URL, params=parameter)
        print(r.json()['code'])
        return render(request,'index.html')

有了上面的views.py類方法,我試圖發布數據。並且我有一個現成的HTML表單可供登錄使用。

<form class="my-login-form" action="/login" method="post">
{% csrf_token %}
<div class="field-wrap">
<input type="email" name="email" required autocomplete="off" placeholder="Email Id"/>
</div>
<div class="field-wrap">
<input type="password" name="password" required autocomplete="off" placeholder="Password">
</div>
<button class="button button-block"/>Login</button>
<div class="forgot"><a class="user-form-toggle" href="#forgot">Forgot Password?</a></div>
</form>

所以我的難題是如何同時映射基於類的視圖和html表單。 現在看來html表單是獨立的!

您應該檢查djangorestframework。 這是一個很好的框架,它通過執行大多數配置而不是代碼來定義具有多種格式,過濾器等的REST API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM