【发布时间】:2021-02-01 14:21:27
【问题描述】:
我有一个网页,在这个网页上我有一个按钮。我正在尝试获取它,以便当用户单击此按钮时,它将调用 API 端点并更新我创建的数据库模型。
如果您需要更多信息,请告诉我,我现在完全迷路了。
sets.html
{% block content %}
<div class="background card">
<div class="card-body">
<button class="btn" id="setRefresh" style="border: 1px solid #555555; color: #555555" onclick="location.href={% url 'setrefresh' %}"><i class="fas fa-sync"></i></button>
</div>
</div>
{% endblock%}
views.py
def sets(request):
return render(request,
"main/sets.html",
{"Sets": Set.objects.all})
def setrefresh(request):
try:
response = requests.request("GET", "https://api.scryfall.com/sets")
data = response.json()
data = data["data"]
for entry in data:
s = Set()
s.scry_id = entry["id"]
s.code = entry["code"]
s.name = entry["name"]
s.set_type = entry["set_type"]
s.release_date = entry["released_at"]
s.card_count = entry["card_count"]
s.digital_only = entry["digital"]
s.foil_only = entry["foil_only"]
s.nonfoil_only = entry["nonfoil_only"]
s.icon = entry["icon_svg_uri"]
s.status = 0
s.save()
return HttpResponse("ok")
except Exception:
return HttpResponseBadRequest()
urls.py
urlpatterns = [
path('', views.dashboard, name="dashboard"),
path('sets', views.sets, name="sets"),
path('cards', views.cards, name="cards"),
url(r'^setrefresh/', views.setrefresh, name='setrefresh'),
]
目录树
【问题讨论】:
-
可以直接分享你的tree吗?
标签: html python-3.x django function view