Admin form_url breakout problem

85 views
Skip to first unread message

Mike Dewhirst

unread,
Jan 23, 2019, 1:57:34 AM1/23/19
to Django users
How can I break out of the Admin to a payment gateway? I've tried form_url='billing:payment_view'  in SubstanceAdmin.change_view() unsuccessfully.

I can semi-break out of the admin and get the beginning of a payment happening with the following ...

SubstanceAdmin.change_form_template = payment.html (which has Stripe js and necessary hidden fields)
or
./templates/admin/substance/substance/change_form.html (identical to payment.html except it calls block.super at the end to render the rest of the admin page)

SubstanceAdmin.change_view() which collects the contextual data required for either template

IngredientsInline.form (inherits  admin.StackedInline.form) for all the hidden fields for each ingredient.

With all that, if an ingredient hasn't been paid for, the billing.Subscription record won't have a Stripe token so the Admin will launch the payment page. On entering credit card detail and pressing the button, Stripe happily takes the money and issues a token. BUT the Admin then barfs with ...


RuntimeError at /admin/substance/substance/1442/change/payment

You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to localhost:8000/admin/substance/substance/1442/change/payment/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

... If I add a slash in the browser address bar and press Enter the next problem emerges ...


ValueError at /admin/substance/substance/1442/change/payment/change/

invalid literal for int() with base 10: '1442/change/payment'

... which tells me the Admin is hoping for /admin/substance/substance/1442/

I don't know where to go now.

My preference would be to leave the Admin somehow and handle the payment externally with my
billing.payment_view() and billing.success_view() then at the end find a way to give that hoped-for url to the server and expect the substance to come up normally in the admin.

Any advice will be greatly appreciated. Happy to show any code you might want to see.

Many thanks

Mike





Matthew Pava

unread,
Jan 23, 2019, 9:09:20 AM1/23/19
to django...@googlegroups.com

It looks like your second URL is different than the first.

/admin/substance/substance/1442/change/payment

/admin/substance/substance/1442/change/payment/change/

 

If you just want to add a slash, then add it.

/admin/substance/substance/1442/change/payment/

 

I’m guessing that Django is interpreting the second URL like so:

/admin/substance/substance/(pk=1442/change/payment)/change/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8061a7d-27b5-c929-c364-d8870829ad3e%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Jan 23, 2019, 4:48:25 PM1/23/19
to django...@googlegroups.com
On 24/01/2019 1:08 am, Matthew Pava wrote:
>
> It looks like your second URL is different than the first.
>
> */admin/substance/substance/1442/change/payment*
>
> */admin/substance/substance/1442/change/payment/change/*
>
> If you just want to add a slash, then add it.
>
> */admin/substance/substance/1442/change/payment/*
>
> I’m guessing that Django is interpreting the second URL like so:
>
> */admin/substance/substance/(pk=1442/change/payment)/change/*
>

You are right. It looks like Django expects the last element of the url
to be the id and automatically appends 'change' to a modelAdmin url.
That apparently invokes the 'change' form.

My mission (I think) is to find a way to launch a non-Admin url from
inside the Admin. Or perhaps from a model method.

That is what I meant with the word 'breakout' in the subject line.

Derek suggested a modelAdmin wrapper a few days ago ... "Basically you
can create your own view and use that to display data in a template that
inherits from, for example, the Django admin form template. A bit tricky
first time but then it seems straightforward."

I'm finding it a bit tricky for sure!

If you can see a way ...

Thanks

Mike

> *From:*django...@googlegroups.com
> [mailto:django...@googlegroups.com] *On Behalf Of *Mike Dewhirst
> *Sent:* Wednesday, January 23, 2019 12:57 AM
> *To:* Django users
> *Subject:* Admin form_url breakout problem
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/f8061a7d-27b5-c929-c364-d8870829ad3e%40dewhirst.com.au?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d0ae4053cd254b2bbc982cff3637d0a6%40iss2.ISS.LOCAL
> <https://groups.google.com/d/msgid/django-users/d0ae4053cd254b2bbc982cff3637d0a6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>.

Matthew Pava

unread,
Jan 23, 2019, 5:35:59 PM1/23/19
to django...@googlegroups.com
Have you tried setting APPEND_SLASH = False in your settings?

-----Original Message-----
From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Mike Dewhirst
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1a6d5511-1273-1420-4481-959813a13654%40dewhirst.com.au.

Mike Dewhirst

unread,
Jan 23, 2019, 6:24:05 PM1/23/19
to django...@googlegroups.com
On 24/01/2019 9:35 am, Matthew Pava wrote:
> Have you tried setting APPEND_SLASH = False in your settings?

Yes. The result is ...


Page not found(404)

Request Method: POST
Request URL:
http://localhost:8000/admin/substance/substance/1442/change/payment

Using the URLconf defined in|ssds.urls|, Django tried these URL
patterns, in this order:


... followed by all the urls the most pertinent one of which is ...

^admin/ ^substance/substance/ ^(.+)/change/$
[name='substance_substance_change']

Matthew Pava

unread,
Jan 24, 2019, 10:41:28 AM1/24/19
to django...@googlegroups.com
Hi Mike,
I'm not really seeing why this is throwing errors at you. It seems like you've done everything right. Could you provide the code (or the relevant parts) for the Substance Admin form?
Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/561d6858-5a38-ae16-43b8-cea64dc00aaf%40dewhirst.com.au.

Mike Dewhirst

unread,
Jan 24, 2019, 5:38:23 PM1/24/19
to django...@googlegroups.com
On 25/01/2019 2:40 am, Matthew Pava wrote:
> Hi Mike,
> I'm not really seeing why this is throwing errors at you. It seems like you've done everything right.

That's very generous of you :)

I got it mostly working last night after patching some code which was
throwing errors. There's a bit of robustness to add before I'm properly
confident but it seems within reach now.

Thanks for listening. When I have it all working I'll try and put
together an Admin recipe for the use-case of detecting a required
payment and wheeling in an external payment gateway.

The scenario here is that the m2m record relating Substance to itself to
form mixtures has a save method which detects whether it is free
(essentially open information) or is a revenue raiser for the
information curator. That triggers the Admin change_view() which calls
the payment gateway via a billing system I wrote for another project
(paid online training) and modified for this system. substance.admin.py
is 2500+ LOC and there are 40+ models in the substance app.

Thanks again

Mike

Mike Wyatt

unread,
Jan 24, 2019, 7:28:25 PM1/24/19
to django...@googlegroups.com
HI, I think you have the wrong Mike here. Although I use Django, None of these issues are ones that I can 1) help with or 2) am dealing with now. 
Mike

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
M-

Mike Dewhirst

unread,
Jan 25, 2019, 4:35:55 AM1/25/19
to django...@googlegroups.com
On 25/01/2019 2:40 am, Matthew Pava wrote:
Hi Mike,
I'm not really seeing why this is throwing errors at you.  It seems like you've done everything right.  Could you provide the code (or the relevant parts) for the Substance Admin form?
Thanks!

Matthew

I'm still in trouble and including some code. I'm not being precious about it just hoping not to overwhelm you. Please ask for the next instalment ...

class SubstanceAdmin(admin.ModelAdmin):

    def subscribe(self, sm2mi, fee_type, fullyear=False):
        """
        sm2mi is the substance<-ingredient m2m through record
        fee_type determines the subscription fee amount
        fullyear=False means calculate the fee pro-rata to 30 September

        We don't need a request here so this can be imported from elsewhere.
        Only called if a fee is payable. Only returns a subscription if no token or
        fullyear == True meaning a subscription renewal is due
        """
        subscription, new = Subscription.objects.get_or_create(
            licensee=sm2mi.substance.division.company,
            ingredient=sm2mi.ingredient,
            fee_type=fee_type,
        )
        if not subscription.token or fullyear:
            return subscription

    def change_view(self, request, object_id, form_url='', extra_context=None):
        """
        For the billing system we want to include all the context data so the
        change_form populates itself properly. See collect_content
        self = SubstanceAdmin
        request = wsgi request object
        object_id = substance
        form_url = no idea!
        extra_context = no_idea
        """
        sm2mis = Substance_Ingredients.objects.filter(substance_id=object_id)
        for sm2mi in sm2mis:
            payable, fee_type = sm2mi.fee_payable()  # eg., True, PAID_DATA
            if payable:
                subscription = self.subscribe(sm2mi, fee_type)
                if subscription:    # we need to collect money for the owner
                    self.change_form_template = 'payment.html'
                    content = collect_content(
                        sm2mi,
                        subscription,
                    )
                    return payment_view(
                        request,
                        sm2mi,
                        subscription,
                        content=content
                    )

        return super(SubstanceAdmin, self).change_view(
            request, object_id, form_url, extra_context=extra_context,
        )

Here is the payment form ... which is further down in admin.py

    class IngredientsInline(admin.StackedInline):

        class PaymentForm(admin.StackedInline.form):
            sm2mi_id = forms.CharField(widget=forms.HiddenInput, required=False)
            ingredient_id = forms.CharField(widget=forms.HiddenInput, required=False)
            subscription_id = forms.CharField(widget=forms.HiddenInput, required=False)
            licensee_id = forms.CharField(widget=forms.HiddenInput, required=False)

            stripeToken = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeEmail = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeBillingName = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeBillingAddressLine1 = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeBillingAddressZip = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeBillingAddressState = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeBillingAddressCity = forms.CharField(widget=forms.HiddenInput, required=False)
            stripeBillingAddressCountry = forms.CharField(widget=forms.HiddenInput, required=False)

        form = PaymentForm

All of the above is working so far as launching the Stripe javascript popup and generating the receipt record correctly and so on but does not return to the admin nor render the success page.

The try/except block in payment_view() barfs as follows ...

            content['sm2mi'] = sm2mi
            content['receipt'] = receipt
            content['subscription'] = subscription
            content['message'] = mark_safe(display_message)
            content['link'] = '/admin/substance/substance/{0}/change/'.format(
                sm2mi.substance.id
            )
            # report success to the card payer
            try:
                return render(
                    template_name='success.html',
                    context=content,
                    request=request,
                )
            except Exception as err:
                print('\n327 billing.views %s' % err)


327 billing.views Reverse for 'admin' not found. 'admin' is not a valid view function or pattern name.


Thanks for looking at it

Mike








Matthew Pava

unread,
Jan 25, 2019, 10:02:23 AM1/25/19
to django...@googlegroups.com

Based on the error message, it looks like you’re trying to call reverse(‘admin’) somewhere, but that’s not in the code you shared.  Actually, you probably want to use a reverse call when populating content[‘link’].

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Mike Dewhirst
Sent: Friday, January 25, 2019 3:35 AM
To: django...@googlegroups.com
Subject: Re: Admin form_url breakout problem

 

On 25/01/2019 2:40 am, Matthew Pava wrote:

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Mike Dewhirst

unread,
Jan 25, 2019, 11:26:16 PM1/25/19
to django...@googlegroups.com
Matthew

I think I'll back everything out and start again. There must be a basic
blunder somewhere.

Just talking it through, I can definitely launch payment_view from the
Admin when all the ducks are in a line. That view populates the hidden
..._id fields and Stripe API fields in the form then calls the Stripe
API which initiates the  Stripe javascript in the Payment template which
resubmits the request with POST direct to Stripe so it can gather the
card detail and send back the json containing the token which proves the
card worked. At that point payment is made and my payment_view has lost
all the context because Stripe made the request not Django. payment_view
detects the Stripe response and must reconstruct the model instances
from the data pre-placed in the hidden form fields. Once the instances
are retrieved that payment_view code updates the subscription record,
creates a receipt record , assembles a message for the success_view and
sends an email to the user.

I am having trouble understanding the correct way to launch payment_view
so that Admin doesn't get confused wanting to add payment/ to the url
and thus keeping it all within the Admin.

Regarding your comment on using reverse to derive the link, I agree. I
was just being somewhat paranoid and skipping the reverse code which I
haven't taken the time to study. I will.

Thanks again

Mike


On 26/01/2019 1:59 am, Matthew Pava wrote:
>
> Based on the error message, it looks like you’re trying to call
> reverse(‘admin’) somewhere, but that’s not in the code you shared. 
> Actually, you probably want to use a reverse call when populating
> content[‘link’].
>
> *From:*django...@googlegroups.com
> [mailto:django...@googlegroups.com] *On Behalf Of *Mike Dewhirst
> *Sent:* Friday, January 25, 2019 3:35 AM
> *To:* django...@googlegroups.com
> *Subject:* Re: Admin form_url breakout problem
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/c70b8afa-6c7b-07b8-3f5d-ec2da8047951%40dewhirst.com.au?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/7adfa5a476904a0e884e6e5ab9a42171%40iss2.ISS.LOCAL
> <https://groups.google.com/d/msgid/django-users/7adfa5a476904a0e884e6e5ab9a42171%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>.

Matthew Pava

unread,
Jan 28, 2019, 10:34:57 AM1/28/19
to django...@googlegroups.com
Hi Mike,
When Stripe processes the payment, you need to preventDefault on the form submit, then add the token from Stripe to a hidden input in your form without affecting any other data in the form. Then you can do form.submit() and let the admin handle it from there. Check out the Stripe documentation for some good examples. I've used it myself.

-----Original Message-----
From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Mike Dewhirst
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/57fd8c64-2869-b533-08a2-1f8d6d3df378%40dewhirst.com.au.

Mike Dewhirst

unread,
Jan 30, 2019, 1:14:26 AM1/30/19
to django...@googlegroups.com
Matthew

I looked for preventDefault and found it in lots of javascript scripts.
So I decided instead to look at my python code which is slightly easier
for me to understand. I sort of refactored it and cut large swags out to
simplify and inserted lots of print statements. Then I thought I'd
better read your advice again and try harder to understand it.

The bit which made sense to me was ...

'let the admin handle it from there'

... I noticed that I was returning the billing payment_view. As soon as
I removed that 'return' and let the change_view return the super call it
started working.

Thank you very much for persisting with me.

The Admin is fabulous! Nobel prize for something is warranted :)

Much appreciated

Mike

Mike Dewhirst

unread,
Feb 1, 2019, 11:28:16 PM2/1/19
to django...@googlegroups.com
I thought it was fixed but not so. It works perfectly *only* if the dev
server reloads after all the work and prior to clicking the link on the
success.html template back to the mixture (the substance with ingredients).

I tried a workaround of making the link point to the substances list
page first. No difference. All substance pages come up blank.

When I reported earlier that it was working I must have changed some
text in a view to improve the screen wording and on saving the dev
server would have reloaded itself and that hid the problem.

The admin url is correct  ...

http://localhost:8000/admin/substance/substance/1442/change/

... but that refuses to display. There is no delay, no spinner. The
server console window shows nothing.

Not sure where to start looking.

Maybe something has corrupted Django's in-memory list of url patterns?

Is it caching?

Thanks for any non-javascript hints

Mike

Mike Dewhirst

unread,
Feb 2, 2019, 2:07:38 AM2/2/19
to django...@googlegroups.com
I think I know. Don't waste time on this. I'll report back later.

Mike

Mike Dewhirst

unread,
Feb 3, 2019, 2:43:08 AM2/3/19
to django...@googlegroups.com
Matthew

I was wrong. I'm still in trouble. I thought it might be an incomplete
transaction so I tried making the the subscription.save() atomic but to
no avail.

Here is what happens ...

1.  Add an ingredient which is payable

2.  Save mixture

3.  SubstanceAdmin.change_view() establishes ...

    subscription = None
    if ingredient.is_payable():
        get or create a subscription record and populate it with fee,
gst etc.
        if the subscription.token field is blank (ie not paid):
            call billing_payment_view() which interfaces with Stripe
via a js popup and
                a) gets a token in a hidden field
                b) calls Stripe.charge(token) and gets a json charge
record back
                c) populates the subscription.token field
                d) saves the subscription record
                e) creates a receipt record
                f) sends an email receipt
                g) puts thank you text and mixture url into context dict
                h) calls billing_success_view(request, context) to
display success
                   message including link back to mixture
    return super(SubstanceAdmin, self).change_view()

4.  On clicking the back-to-the-mixture link in success.html, the Admin
brings
    up the Change Substance page without error but which is blank below the
    "Change Substance" heading. At that time the displayed URL in the
address
    bar is exactly correct.

5.  If the dev server reloads the page will paint properly if refreshed.

Theory:

The subscription record has been saved but not in a way which is visible
yet thereby confusing change_view() into thinking the token field is blank.

In other words the transaction needs to be committed
pre-billing_success_view()

In testing this it sadly this makes no difference.

Mike

Matthew Pava

unread,
Feb 4, 2019, 10:21:20 AM2/4/19
to django...@googlegroups.com
The flow looks fine. And when you click refresh on your browser on step 4 does the page refresh properly?

Perhaps it's a caching issue.
Are you wrapping the billing_payment_view with admin_view()?
https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls --> scroll towards the bottom of that section
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4c3993b0-797f-f6cf-8bc2-5bf8b46c5ae2%40dewhirst.com.au.

Mike Dewhirst

unread,
Feb 10, 2019, 10:03:07 PM2/10/19
to django...@googlegroups.com
Matthew

I think a workaround would be to reload the admin urls after each Stripe
transaction. I don't know how to do that after looking at options.py etc.

I know I'm doing something adverse and it is spoiling the admin urls
because the url in the address bar is correct but the page contents is
empty. Refresh and shift-refresh doesn't do anything.

Saving a bit of source to reload runserver fixes (works around) the problem.

I'm happy to reveal as much source as necessary if you would like to see
it and have the time.

Thanks very much for sticking with this

Mike

Mike Dewhirst

unread,
Feb 12, 2019, 8:38:19 PM2/12/19
to Django users
Starting a new thread with the same subject because the old thread ...
https://groups.google.com/forum/#!topic/django-users/YLbWzmPfHwU
... is too long

Progress report.

No change in the symptoms with Python 3.6 and Django 1.11 and Django
runserver.

I deployed to the staging server Ubuntu 16.04, Python 2.7, Django 1.11
and Apache2 to get an error generated by functools.py.

I have decided to bite the bullet and upgrade the staging server to
Python 3.5 (I think) and then Django 2.x. It has to be done sometime.
I'm moving my Trac instances off the staging server to a spare machine
which can remain with Python 2.7 so everything else can go to Py3.x

A side plan is to make a Django 2.1 venv in development to see if that
fixes anything.

More later.

Mike

Mike Dewhirst

unread,
Feb 16, 2019, 1:58:13 AM2/16/19
to django...@googlegroups.com
Django 2.0 makes no difference either so I've gone back to 1.11 for the
time being. To recap ... I'm successfully launching a Stripe payment
page from within the Admin  followed by a success (or failure) page
which is at the following URL ...

http://localhost:8000/admin/substance/substance/1442/change/payment

... with a link back to the original Admin page.

http://localhost:8000/admin/substance/substance/1442/

On clicking that link the Admin shows *no error but no content* other
than the normal top of page at the following correct URL ...

http://localhost:8000/admin/substance/substance/1442/change/

The only way I can get it to show the proper content at that URL is to
reload runserver.

Here are my project urls ...

from __future__ import unicode_literals, absolute_import, division

from django.conf import settings
from django.conf.urls import include
from django.conf.urls import url as re_path  # ready for 1.2
from django.contrib import admin
import django.contrib.auth.views as auth_views
import django.views as django_views

from billing import views as billing_views
from common.views import privacy


admin.autodiscover()
adminurls = admin.site.urls


urlpatterns = [

    re_path(r'^substance/', include('substance.urls')),

    re_path(r'', include('common.urls')),

    re_path(r'payment$',
        billing_views.payment_view,
        name='payment_view'
    ),

    re_path(r'success$',
        billing_views.success_view,
        name='success_view'
    ),


    re_path(r'^admin/password_reset/$',
        auth_views.password_reset,
        name='django.contrib.auth.views.admin_password_reset'),

    re_path(r'^admin/password_reset/done/$',
        auth_views.password_reset_done,
        name='django.contrib.auth.views.password_reset_done'),

re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
        auth_views.password_reset_confirm,
name='django.contrib.auth.views.password_reset_confirm'),

    re_path(r'^reset/done/$',
        auth_views.password_reset_complete,
name='django.contrib.auth.views.password_reset_complete'),

    # Uncomment the admin/doc line below to enable admin documentation:
    re_path(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    re_path(r'^admin/', admin.site.urls),
]

if not settings.APACHE:
    urlpatterns.append(
        re_path(r'^media\/(?P<path>.*)$',
            django_views.static.serve,
            {'document_root': settings.MEDIA_ROOT}),
    )
    urlpatterns.append(
        re_path(r'^static\/(?P<path>.*)$',
            django_views.static.serve,
            {'document_root': settings.STATIC_ROOT}),
    )


I'm using ModelAdmin.change_view as follows ...


    def change_view(self, request, object_id, form_url='',
extra_context=None):
        """
        self = SubstanceAdmin
        request = wsgi request object
        object_id = substance
        form_url = no idea!
        extra_context = dict of apps, models, admin_urls and permissions
        """
        # Substance_Ingredients is a m2m (to self being Substance)
through table #
        ingredients =
Substance_Ingredients.objects.filter(substance_id=object_id)
        subscription = None
        for sm2mi in ingredients:
            payable, fee_type = sm2mi.fee_payable()  # eg., True, PAID_DATA
            if payable:
                subscription = billing_subscribe(sm2mi, fee_type)
                if subscription:    # we need to collect money for the
owner
                    self.change_form_template = 'payment.html'
                    context = billing_collect_context(
                        sm2mi,
                        subscription,
                    )
                    # get everything into the payment_view context
                    if not extra_context:
                        extra_context = dict()
extra_context.update(self.admin_site.each_context(request))
                    extra_context.update(context)
                    self.admin_site.admin_view(
                        # call the Stripe mechanism
                        billing_payment_view(
                            request,
                            sm2mi,
                            subscription,
                            context=extra_context,
                        )
                    )
                    # payment for only one sm2mi at a time
                    break
        return super(SubstanceAdmin, self).change_view(
            request, object_id, form_url, extra_context
        )


 I have tried myriad ways to reload the urls and persist context past
the Stripe form but cannot do it apart from reloading the server.

I'm thinking that maybe the Admin has a mechanism to re-init itself but
I can't find it. I'm out of ideas at the moment so if anyone can help I
would be very appreciative.

I still haven't got Apache working with Python 3.5 mod_wsgi so I can't
say anything there.

Thanks

Mike
Reply all
Reply to author
Forward
0 new messages