0

I have added a custom button to my Django Admin by overriding change_form.html. I'd like this button to read a site_url field from the model itself, scape that site, and then programatically change the model's form fields (without saving it).. so it can be reviewed and corrected before saving.

I have successfully got my button to print the object by overriding response_change. I can also pre-populate the template by using extra_context by overriding change_view.

def response_change(self, request, obj):
    if '_scrape-site' in request.POST:
      print(obj)
      return HttpResponseRedirect(".")
    return super().response_change(request, obj)

I'd like to be able to enter a site_url and scrape it using requests/bs4 or scrapy, and then return those values into the change_form in the Title and Summary text boxes (see https://i.stack.imgur.com/bbskD.png). Is that possible?

Pradip Tilala
  • 1,715
  • 16
  • 24
  • 1
    This is probably best handled by a custom view that returns a redirect to the admin add view. You can use GET parameters to set initial values for fields in the add view `/admin/app/model/add/?title=whatever` – Iain Shelvington Sep 19 '19 at 06:02
  • Thanks @IainShelvington, this strategy worked well for me. I created a new custom admin view, put an `input form` on that view to collect the `site_url`, and then on `request.POST` I scraped the site and sent redirected to the add view with a urlencoded query string. – Stephen Richter Sep 20 '19 at 00:41

0 Answers0