我目前正在开发一个具有更多复杂管理页面的项目。
目前我想要完成的是当用户添加报告时,此报告会检查一堆数据并创建该区域内的人员列表(小于10公里)。因此,当您单击将其更改视图保存到列出其找到的所有人及其电子邮件的视图时,当他们添加报告时,您可以选择要添加的人,然后按另一个按钮来执行更多操作。
我的代码如下:
admin.register(Report)
class ReportAdmin(admin.ModelAdmin):
change_form_template = 'admin/phone/index.html'
# inlines = [SubjectInLine]
def response_change(self, request, obj):
"""
Determines the HttpResponse for the change_view stage.
"""
opts = self.model._meta
msg_dict = {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
context = {}
if "_email" in request.POST:
msg = _('Report Saved - Now please select the store below you would like to notify.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
payload = {'address': str(obj.location.address1) + ' ' + str(obj.location.address2)}
start = requests.get("https://maps.googleapis.com/maps/api/geocode/json", params=payload)
start = start.json()
start_long = start['results'][0]['geometry']['location']['lng']
start_lat = start['results'][0]['geometry']['location']['lat']
start_loc = (float(start_lat), float(start_long))
clients = Clients.objects.filter()
context['report'] = obj
in_ten = []
for c in clients:
payload = {'address': str(c.address1) + ' ' + str(c.address2)}
end = requests.get("https://maps.googleapis.com/maps/api/geocode/json", params=payload)
try:
end = end.json()
end_long = end['results'][0]['geometry']['location']['lng']
end_lat = end['results'][0]['geometry']['location']['lat']
end_loc = (float(end_lat), float(end_long))
distance = vincenty(start_loc, end_loc).kilometers
if (distance < 10 and c.pk != obj.location.pk):
in_ten.append(c)
except:
print(str(c) + " Bad Address")
context["clients"] = in_ten
obj.save()
return render(request,'phone/email.html',context)
elif "_confirm-email" in request.POST:
print ("HELLO")
print (context["report"])
return render(request, 'phone/email.html', context)
else:
msg = _('The %(name)s "%(obj)s" was changed successfully.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
return self.response_post_save_change(request, obj)
def get_actions(self, request):
actions = super(ReportAdmin, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
手机密码/ email.html:
{% extends "admin/base_site.html" %}
{% load i18n admin_urls admin_static admin_modify %}
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
{{ media }}
{% endblock %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />{% endblock %}
{% block coltype %}colM{% endblock %}
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %}
{% block content %}<div id="content-main">
<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
<h1>Email Section</h1>
<div style="height:500px">
<table>
<th>Location</th>
<th>Email</th>
<th>Include in Email?</th>
{% for c in clients %}
<tr>
<td>{{ c.name }}</td>
<td>{{ c.contact_email }}</td>
<td><input type="checkbox" name="{{c.name}}"></td>
</tr>
{% endfor %}
</table>
<div class="submit-row">
<input type="submit" value="{% trans 'Email Confirmation' %}" class="default" name="_email-confirm" />
</div>
</div>
</form></div>
{% endblock %}
所以你可以看到我覆盖了response_change方法然后当我注意到_email按钮被按下时我会渲染一个新页面。现在这确实有效。现在,当渲染这个新模板时,我按下了_email-confirm按钮,它只是从头开始重新加载页面,没有看到打印语句。
任何见解都会很棒。
答案 0 :(得分:0)
所以我想方设法:
我正在使用Django提供的错误方法。
我这样做的方式是:
def change_view(self, request, object_id, form_url='', extra_context=None):
context = {}
if "_email-confirm" in request.POST:
print ("CONFIRM")
return render(request,'phone/confirm.html',context)
if "_continue" in request.POST:
print ("CONTINUE")
'''
obj = Report.objects.get(pk=object_id)
payload = {'address': str(obj.location.address1) + ' ' + str(obj.location.address2)}
start = requests.get("https://maps.googleapis.com/maps/api/geocode/json", params=payload)
start = start.json()
start_long = start['results'][0]['geometry']['location']['lng']
start_lat = start['results'][0]['geometry']['location']['lat']
start_loc = (float(start_lat), float(start_long))
clients = Clients.objects.filter()
context['report'] = obj
in_ten = []
for c in clients:
payload = {'address': str(c.address1) + ' ' + str(c.address2)}
end = requests.get("https://maps.googleapis.com/maps/api/geocode/json", params=payload)
try:
end = end.json()
end_long = end['results'][0]['geometry']['location']['lng']
end_lat = end['results'][0]['geometry']['location']['lat']
end_loc = (float(end_lat), float(end_long))
distance = vincenty(start_loc, end_loc).kilometers
if (distance < 10 and c.pk != obj.location.pk):
in_ten.append(c)
print (c.address)
except:
print(str(c) + " Bad Address")
context["clients"] = in_ten
'''
return render(request,'phone/email.html',context)
#return super(ReportAdmin, self).change_view(request, object_id, 'phone/email.html', context)
else:
return super(ReportAdmin, self).change_view(request,object_id,form_url)
所以我在这里拦截了change_view并确保渲染我想要的页面。
希望这有助于其他人。