Django url解析器

时间:2017-12-01 09:17:13

标签: python django django-forms django-admin django-urls

在模板的这一行:

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

我得到了:

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

练习/ 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)

管理网站中的工作网址应如下所示:

  

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

我不知道在哪里查看,或者更改以修复此错误。

谢谢@Sandeep Balagopal! 为什么我没有想到grep -rl "smart_admin_urlname" .愚蠢的我。

  

公共/ 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)
  

练习/ 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

添加以下行修复了链接,但这不是我想要做的...它使管理网站显示Exercise类。

site.register(Exercise, ExerciseAdmin)

尝试过,但没有任何区别:

  

练习/ 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:model = ', <class 'exercises.models.PythonExercise'>)
('DEBUG:model = ', <class 'exercises.models.SqlExercise'>)

0 个答案:

没有答案