我对编程很陌生,有一件事我很困惑,我的老板让我创建一个关于应用程序的rest,我已经完成了所有的注册、登录和其他部分。但是现在我必须创建一个前端,但是互联网上的所有教程都是关于创建Django的rest以作出反应,或者其他的前端框架,是他们关于YouTube的任何教程,关于连接到前台的html到Rest。谢谢。
发布于 2022-02-12 08:36:03
如果您不想要一个前端,只需按照django的正式教程学习如何从django提供html/css/js。
在那里,您可以通过django上下文传递您需要的api中的数据,也可以使用fetch或类似的东西从javascript获取数据。
后者看起来会是这样。
# views.py
def index(request):
return render(request, 'index.html')
def apiview(request):
return JsonResponse({'title', 'served from api'})index.html
<div class="title" token={{csrf_token}}></div>
<script>
function getTitle() {
const div = document.querySelector('title')
const csrf_token = div.getAttribute('token')
fetch('localhost:8000/apiview', {
method: 'get',
headers: {'X-CSRFToken': token}
})
.then(data => data.json())
.then(data => div.innerHTML = data.title)
}
</script>https://stackoverflow.com/questions/71090033
复制相似问题