# urls.py
from django.urls import path
from .views import address_lookup
urlpatterns = [
# other patterns
path('address_lookup/', address_lookup, name='address_lookup'),
]
型 最后需要编写JavaScript来处理表单输入:
<script>
// Add event listeners to input fields
document.getElementById('postcode').addEventListener('input', updateAddress);
document.getElementById('house_number').addEventListener('input', updateAddress);
function updateAddress() {
// Get values from input fields
var postcode = document.getElementById('postcode').value;
var houseNumber = document.getElementById('house_number').value;
// Make an Ajax request to the server
var xhr = new XMLHttpRequest();
xhr.open('GET', '/address_lookup/?postcode=' + postcode + '&house_number=' + houseNumber, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Update the displayed address
document.getElementById('fullAddress').innerText = JSON.parse(xhr.responseText).full_address;
}
};
xhr.send();
}
</script>
1条答案
按热度按时间mw3dktmi1#
我找到了一个解决办法:
首先需要在Django视图中添加一个端点:
字符串
然后需要为新视图创建一个URL模式:
型
最后需要编写JavaScript来处理表单输入:
型