0
我目前正在開發一個具有更多複雜Admin頁面的項目。創建具有多個1視圖的ModelAdmin
目前我正在努力完成的是當用戶添加一個報告這個報告,然後檢查一堆數據並創建一個區域內(不到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按鈕,它只是從頭開始重新加載頁面,並且沒有看到打印語句。
任何見解都會很棒。