Okay, I've got such a button and I have copied a template from admin to serve as a stand-in for it. I have created functions under ProvinceAdmin (in myapp\admin.py) that look like:
** code ** def ProvinceAdmin(admin.ModelAdmin): # other stuff def add_random_samurai(self, request): from django.http import HttpResponse # do stuff return HttpResponse("This is where the template will render.") def get_urls(self): from django.conf.urls.defaults import patterns urls = super(ProvinceAdmin, self).get_urls() my_urls = patterns('', (r'^/(?P<province_id>\d+)/add_random_samurai/$', self.admin_site.admin_view(self.add_random_samurai)) ) return my_urls + urls ** / code ** But I'm getting a ValueError: invalid literal for int() with base 10: '1/add_random_samurai' If I hardcode: (r'^/1/add_random_samurai/$', self.admin_site.admin_view(self.add_random_samurai)) Then it works. So I think my regular expression is not picking up the right integer. Can you help me re-write the regular expression to catch the province number? Or can I ignore the province number since I am presumably calling a function from within the instance of the province I want to update? If so, how do I tell the pattern matching to ignore that? -Tim On Fri, Feb 19, 2010 at 5:21 PM, Peter Herndon <tphern...@gmail.com> wrote: > > On Feb 19, 2010, at 5:51 PM, Timothy Kinney wrote: > > > So I have a nice little database now stocked with items and provinces. > > I now want to generate random samurai and populate the database with > > them. I can think of two ways to do this: > > > > 1) Through the admin interface. But how do I install a button that > > will add a random samurai? Adding samurai is a built-in function on > > the template, but the fields are always empty. Is there a > > straightforward way to add another button called "Add Random Samurai" > > that does the same thing but with the fields randomly filled from > > appropriate choices? > > > > 2) Use a python script. This seems to have two possible methods: > > a) Randomly generate samurai desired, output a JSON flatpage, and call > > manage.py loaddata that_flatpage > > > > b) Randomly generate the samurai desired, access the database directly > > and insert them using SQL syntax. (not very Django like) > > > > I have listed these in order of preference. Can someone tell me the > > easiest way to implement a new admin button? I'm not even sure where > > the admin templates are stored. :/ > > > > -Tim > > Hi Tim, > > Docs for overriding admin are here: > http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#overriding-admin-templates > > You will probably want to override the change_form.html template at > whatever level is appropriate for your needs (Province, if you are adding > Samurai to random rooms), and add a button "Generate Random Samurai". That > button will be the submit for a form that points to a view you will write. > That view should generate a random number, loop over that number, create a > Samurai object and assign it to a randomly-chosen Room (pick a random number > from 1 through the total number of rooms, get the room via "room = > Room.objects.get(pk=<random number>"). You will need to add a URL that will > tie together the view and the submit button. > > For an added bonus, add an IntegerField to your form allowing you to set an > upper bound on the number of Samurai generated. > > An approach similar to 2b would be to implement a custom management command > (skeletal docs here: > http://docs.djangoproject.com/en/1.1/howto/custom-management-commands/#howto-custom-management-commandsbut > Google for better examples) that would allow you to run "python > manage.py create_samurai". That command would use the same logic as I > outlined for the view, and create Samurai via the ORM and assign them to > random Rooms. > > ---Peter Herndon > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.