django - Creating ModelAdmin that has more then 1 view -
i'm working on project has more of complex admin page.
currently i'm trying accomplish when user adds report report checks bunch of data , create list of people within area (less 10 km). when add report when click save changes view view lists people found , emails, can select people want add , press button more stuff.
my code follows:
admin.register(report) class reportadmin(admin.modeladmin): change_form_template = 'admin/phone/index.html' # inlines = [subjectinline] def response_change(self, request, obj): """ determines httpresponse 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 - please select store below 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 = [] 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 , 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" 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
code of phone/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> {% 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 %}
so can see override response_change method , when notice _email button press render new page. work. when new template rendered, , press _email-confirm button reloads page beginning , no print statements seen.
any insight great.
so figured way around this:
i working wrong method provided django.
the way did by:
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 = [] 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 , 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)
so here did intercept change_view , made sure render page wanted.
hope helps others.
Comments
Post a Comment