0

On this line in the template:

{% url object|smart_admin_urlname:'change' object.pk %}

I got:

Exception Value: Reverse for 'exercises_exercise_change' with arguments '(3,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

exercises/admin.py

from django.contrib.admin import site, ModelAdmin

class ExerciseAdmin(ModelAdmin):
    actions = ['apply_tag', 'show', 'hide']
    list_filter = ['exercise_set', 'visible', 'exercise_set']
    list_display = ['short_title', 'visible', 'exercise_set']
    # ommited custom functions and irrelavent parameters

class PythonExerciseAdmin(ExerciseAdmin):
    form = PythonExerciseAdminForm

class SqlExerciseAdmin(ExerciseAdmin):
    form = SqlExerciseAdminForm

site.register(PythonExercise, PythonExerciseAdmin)
site.register(SqlExercise, SqlExerciseAdmin)

The working url in administration site should look like:

http://127.0.0.1/admin/exercises/pythonexercise/3/ http://127.0.0.1/admin/exercises/sqlexercise/4/

I'm not sure where to look, or change to fix this bug.


Thank you @Sandeep Balagopal! Why didn't I think of grep -rl "smart_admin_urlname" . silly me.

common/templatetags/common_tags.py

@register.filter
def smart_admin_urlname(obj, view_name):
    return u'admin:{0}_{1}_{2}'.format(obj._meta.app_label,
                                       obj._meta.model_name, view_name)

exercises/models.py

class Exercise(Model):
    # ommited custom parameters and functions

class PythonExercise(Exercise):
    exercise_type = Constant.PYTHON
    # ommited custom parameters 

class SqlExercise(Exercise):
    exercise_type = Constant.SQL

Adding the following line fixed the link, but this is not what I want to do...it makes the admin site show the Exercise class.

site.register(Exercise, ExerciseAdmin)

Tried, but did not make any difference:

exercises/forms.py

def get_admin_form(exercise_model=PythonExercise,exercise_type=Constant.PYTHON):
    class ExerciseAdminForm(ModelForm):
        class Meta:
            model = exercise_model
            fields = '__all__'

        def __init__(self, *args,**kwargs):
            super(ExerciseAdminForm, self).__init__(*args,**kwargs)
            self.fields['starter_code'].widget = _make_language_mode(exercise_type=exercise_type)
            if exercise_type == Constant.PYTHON:
                # omitted
    print('DEBUG:model = ',ExerciseAdminForm.__dict__['_meta'].__dict__['model'] )               
    return ExerciseAdminForm

Debug message shows:

('DEBUG:model = ', <class 'exercises.models.PythonExercise'>)
('DEBUG:model = ', <class 'exercises.models.SqlExercise'>)
Mzq
  • 1,796
  • 4
  • 30
  • 65
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Dec 01 '17 at 09:22
  • Please show your smart_admin_urlname filter as well. – Sandeep Balagopal Dec 01 '17 at 09:30
  • I don't know where to find this 'smart' admin... – Mzq Dec 01 '17 at 09:33
  • 1
    So the smart_admin_urlname is a custom template filter. It must be defined somewhere in the templatetags/ directory. – Sandeep Balagopal Dec 01 '17 at 09:51
  • You haven't shown how you have registered your models with your model admin classes. Your `smart_admin_urlname` looks as if it is duplicating the functionality of the [`admin_urlname`](https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#reversing-admin-urls) tag in the admin app. – Alasdair Dec 01 '17 at 10:54
  • 1
    You haven't registered a model admin for the `Excercise` model, so `{% url object|smart_admin_urlname:'change' object.pk %}` isn't going to work if `object` is an `Exercise` instance. You need to use a `PythonExercise` or `SqlExercise` instance. – Alasdair Dec 01 '17 at 11:35

0 Answers0