[Fixed]-Creating ModelAdmin that has more then 1 view

1👍

So I figured a way around this:

I was working with the wrong method provided by Django.

The way I did this was 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 = []
        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)

So here what I did was intercept the change_view and made sure to render the page I wanted.

Hope this helps others.

Leave a comment