Django custom urls not defined

I am trying to add two urls to the model admin.

class JobTitleAdmin(admin.ModelAdmin): inlines = [OccupationTagInline, ] model = JobTitle search_fields = ['title',] list_filter = ['status',] actions =['add_to_job_category', 'move_to_job_category' ] def add_to_job_category_view(self, request): return render_to_response( 'admin/job_title/select_job_category.html', { 'action': 'add', 'featured_occupations': Occupation.objects.filter(featured=True), 'title_ids': array(request.GET['foo']) }, context_instance=RequestContext(request) ) def get_urls(self): urls = super(JobTitleAdmin, self).get_urls() custom_urls = patterns('', url(r'^add_to_job_category/$', self.admin_site.admin_view(self.add_to_job_category_view), name='admin_jobtitle_add_to_cat',), url(r'^move_to_job_category/$', self.admin_site.admin_view(self.move_to_job_category_view), name='admin_jobtitle_move_to_cat',) ) return custom_urls + urls 

I skipped the view code for simplicity.

These 2 patterns are not defined since I get 404 if I try to access them and they are not listed in the django debud 404 page. However, the get_urls method is correctly called (twice actually, not sure why).

What could be wrong?

+4
source share
2 answers

I did this successfully, but I copied the ModelAdmin.get_urls wrap decorator. Perhaps try:

 from functools import update_wrapper class JobTitleAdmin(admin.ModelAdmin): inlines = [OccupationTagInline, ] model = JobTitle search_fields = ['title',] list_filter = ['status',] actions =['add_to_job_category', 'move_to_job_category' ] def add_to_job_category_view(self, request): return render_to_response( 'admin/job_title/select_job_category.html', { 'action': 'add', 'featured_occupations': Occupation.objects.filter(featured=True), 'title_ids': array(request.GET['foo']) }, context_instance=RequestContext(request) ) def get_urls(self): urls = super(JobTitleAdmin, self).get_urls() def wrap(view): def wrapper(*args, **kwargs): return self.admin_site.admin_view(view)(*args, **kwargs) return update_wrapper(wrapper, view) custom_urls = patterns('', url(r'^add_to_job_category/$', wrap(self.add_to_job_category_view), name='admin_jobtitle_add_to_cat',), url(r'^move_to_job_category/$', wrap(self.move_to_job_category_view), name='admin_jobtitle_move_to_cat',) ) return custom_urls + urls 
+2
source

From your code, it seems like you're trying to add intermediate action views to your ModelAdmin class.

This is achieved by putting the function name in the actions attribute. If you just add a custom action, there is no need to add a custom URL. Your example can be changed as follows:

 class JobTitleAdmin(admin.ModelAdmin): inlines = [OccupationTagInline, ] model = JobTitle search_fields = ['title',] list_filter = ['status',] actions =['add_to_job_category', 'move_to_job_category' ] def add_to_job_category(self, request, queryset): return render_to_response( 'admin/job_title/select_job_category.html', { 'action': 'add', 'featured_occupations': Occupation.objects.filter(featured=True), 'title_ids': array(request.GET['foo']) # <-- this would probably be changed to use `queryset` function argument }, context_instance=RequestContext(request) ) add_to_job_category.short_description = "Add to job category" 

The ModelAdmin class will know how to call your custom function and add an action to the expansion of the action. The intermediate view of the add_to_job_category response add_to_job_category to know how to receive data from the action in order to determine which items have been selected.

If you really want to add a custom URL, your get_urls() function will prove to be correct. Note that the URLs returned by get_urls refer to admin/<app>/<model> .

If your application was named myapp and your JobTitle model, then the URL of your add_to_job_category will look like this:

 http://some.site/admin/myapp/jobtitle/add_to_job_category/ 
0
source

All Articles