Python Django管理添加表单ajax调用
因此,我以前在Django/Ajax/Jquery方面取得过成功,但我想知道是否可以在管理控制台中实现这一点。当用户转到“添加新”表单时,我希望他们填写邮政编码,并基于该邮政编码,根据数据库中与该邮政编码对应的值,在下一个字段中过滤可用选项 我正在尝试寻找管理视图,该视图为该表单处理帖子,但我对django比较陌生,很难找到它。也许我想得太多了,有更简单的解决办法吗Python Django管理添加表单ajax调用,python,ajax,django,django-admin,django-views,Python,Ajax,Django,Django Admin,Django Views,因此,我以前在Django/Ajax/Jquery方面取得过成功,但我想知道是否可以在管理控制台中实现这一点。当用户转到“添加新”表单时,我希望他们填写邮政编码,并基于该邮政编码,根据数据库中与该邮政编码对应的值,在下一个字段中过滤可用选项 我正在尝试寻找管理视图,该视图为该表单处理帖子,但我对django比较陌生,很难找到它。也许我想得太多了,有更简单的解决办法吗 创建一个视图,在给定zipcode的情况下,该视图包含可用选项(python) 用于具有邮政编码字段(模板)的型号 向zipcod
from django.conf.urls import url
app_name = 'your_app'
urlpatterns = [
url(r'^query/page/(?P<otherModelSlug>[a-zA-Z0-9]+)/?', PageYouQueryFrom.as_view(), name='query_page'),
…
# yourWebsite.com/your_app/util/kittens
url(r'^util/(?P<fieldValue>[a-zA-Z0-9]+)/?', GetDataUtilitly.as_view(), name='get_data_util'),
]
第\u页您希望\u至\u ajax.html
{% extends "base.html" %}
{% block script-inline %}
$(document).ready( function () {
$("#place_to_query").change(function() {
var queryURL = "/your_app/util/" + $(this).val();
queryBox = $(this)
$.get({
url: queryURL,
contentType: "application/x-www-form-urlencoded",
})
.done(function( data ) {
html = $( data ).text()
queryBox.parent().after(html)
});
});
});
{% endblock script-inline %}
{% block content %}
{{ otherModel }} <!-- or it could be a specific field -->
<input id="place_to_query"></input>
<!-- The ajax will go here afterwards-->
{% endblock content %}
数据查询视图-GetDataUtilitly.py
from django.views.generic import TemplateView
import requests
from pyquery import PyQuery
# this is a Class Based View, but you can use a function based one too
class GetDataUtilitly(TemplateView):
template_name = 'path/to/data_template.html'
def get_context_data(self, **kwargs):
field_value = self.kwargs['fieldValue']
page = requests.get('https://www.page.com?searchField=' + field_value)
pq = PyQuery(page.content)
# Get the (html) portion of what you want
result_html = pq('div.c-table__row').html()
return {'result_html': result_html}
,我应该在哪里创建视图?我可以在管理控制台中为特定页面编写自己的视图吗?url(r'^/admin/denate/pickupschedule/add/',“path.to.view?”)。那会弄糟什么吗?你把风景放在哪里都不重要。就我个人而言,我会把它放在保存zipcode模型的应用程序中,所以它看起来更像/zipcodes/suggest/或类似的东西
# this is a Class Based View, but you can use a function based one too
class PageYouQueryFrom(TemplateView):
template_name = 'path/to/page_you_want_to_ajax.html'
def get_context_data(self, **kwargs):
context_data = super(PageYouQueryFrom, self).get_context_data(**kwargs)
context_data['otherModel'] = get_object_or_404(OtherModel, slug=self.kwargs['otherModelSlug'])
return context_data
from django.views.generic import TemplateView
import requests
from pyquery import PyQuery
# this is a Class Based View, but you can use a function based one too
class GetDataUtilitly(TemplateView):
template_name = 'path/to/data_template.html'
def get_context_data(self, **kwargs):
field_value = self.kwargs['fieldValue']
page = requests.get('https://www.page.com?searchField=' + field_value)
pq = PyQuery(page.content)
# Get the (html) portion of what you want
result_html = pq('div.c-table__row').html()
return {'result_html': result_html}