NoReverseMatch error raise when adding a new entry in django admin

135 views
Skip to first unread message

Weifeng Pan

unread,
Feb 11, 2018, 10:20:10 PM2/11/18
to Django users
I got this wired issue. who can help.

Python 3.6
Django Latest 2.0.2

------------following are stack trace---------------
Internal Server Error: /admin/nmm_tokenservice/userprofile/add/ Traceback (most recent call last): File "C:\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner response = get_response(request) File "C:\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Python36\lib\site-packages\django\contrib\admin\options.py", line 574, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) File "C:\Python36\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\Python36\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "C:\Python36\lib\site-packages\django\contrib\admin\sites.py", line 223, in inner return view(request, *args, **kwargs) File "C:\Python36\lib\site-packages\django\contrib\admin\options.py", line 1553, in add_view return self.changeform_view(request, None, form_url, extra_context) File "C:\Python36\lib\site-packages\django\utils\decorators.py", line 62, in _wrapper return bound_func(*args, **kwargs) File "C:\Python36\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\Python36\lib\site-packages\django\utils\decorators.py", line 58, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2) File "C:\Python36\lib\site-packages\django\contrib\admin\options.py", line 1450, in changeform_view return self._changeform_view(request, object_id, form_url, extra_context) File "C:\Python36\lib\site-packages\django\contrib\admin\options.py", line 1495, in _changeform_view return self.response_add(request, new_object) File "C:\Python36\lib\site-packages\django\contrib\admin\options.py", line 1098, in response_add current_app=self.admin_site.name, File "C:\Python36\lib\site-packages\django\urls\base.py", line 88, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "C:\Python36\lib\site-packages\django\urls\resolvers.py", line 632, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'nmm_tokenservice_userprofile_change' with arguments '('',)' not found. 1 pattern(s) tried: ['admin/nmm_tokenservice\\/userprofile\\/(?P<object_id>.+)\\/change\\/$']

---------------- here is my model-------------------
class UserProfile(models.Model):
#base information
user = models.OneToOneField(User, on_delete=models.deletion.CASCADE, verbose_name='系统账户', related_name='profile')
uid = UIDField(verbose_name='用户ID', primary_key=True, editable=False, null=False, blank=False)
imuid = UIDField(verbose_name='IM用户ID', editable=False, null=True, blank=True)
nickname = models.CharField(verbose_name='昵称', max_length=20, null=True, blank=True, db_index=True)
tel = models.CharField(verbose_name='手机号码', max_length=20, null=True, blank=False)
#status and level
status = models.CharField(verbose_name='状态', max_length=20, null=False, blank=False, default=USER_STATUS[0][0], choices=USER_STATUS)
level = models.PositiveIntegerField(verbose_name='级别', null=False, blank=False, default=1, db_index=True)
#org information
city = CityCodeField(verbose_name='城市', null=True, blank=False)
org = CodeField(verbose_name='学校或组织机构代码', null=True, blank=True, db_index=True)
textbookcode = LabelCodeField(verbose_name='教材(标签代码)', help_text='请填写标签代码', null=True, blank=True, db_index=True)
gradecode = LabelCodeField(verbose_name='年级/级别(标签代码)', help_text='请填写标签代码', null=True, blank=True, db_index=True)
#role
role = UserRoleField(verbose_name='角色', null=False, blank=False, choices=UserRole_CHOICES, default=UserRole.user.name)
### teacher specific attributes ###
synopsis = models.TextField(verbose_name='个人简介', max_length=500, null=True, blank=True)
workhistory = models.TextField(verbose_name='工作经历', max_length=500, null=True, blank=True)
cert = models.TextField(verbose_name='荣誉证书', max_length=500, null=True, blank=True)
labels = models.CharField(verbose_name='个人标签', max_length=100, null=True, blank=True)
scope = models.CharField(verbose_name='出题范围', help_text='老师的出题范围,仅对老师角色有效', max_length=100, null=True, blank=True)
title = models.PositiveIntegerField(verbose_name='头衔级别', null=False, blank=False, default=1, db_index=True)
### promotion and relation ###
promotioncode = PromotionCodeField(verbose_name='推荐码', help_text='用于推荐其他用户或者老师', null=True, blank=True)
broker_promote = UIDField(verbose_name='推荐者用户ID', null=True, blank=True, db_index=True)
broker_firstserve = UIDField(verbose_name='首服老师用户ID', null=True, blank=True, db_index=True)

class Meta:
verbose_name = '用户信息'
verbose_name_plural = '用户信息'

-----------here is the admin class---------------
class UserProfileAdmin(admin.ModelAdmin):
# list page
list_display = ('date_joined', 'uid', 'user', 'tel', 'imuid', 'nickname', 'role', 'city', 'level', 'title', 'status', 'promotioncode', 'broker_promote', 'broker_firstserve', )
list_display_links = ('uid', )
ordering = ('uid', )
search_fields = [
'=uid',
'=tel',
'nickname',
'user__username',
]
list_filter = ('role', 'city', 'level', 'title', 'status', )

def date_joined(self, obj):
return obj.user.date_joined

date_joined.admin_order_field = 'user__date_joined'
date_joined.short_description = '注册日期'

def save_model(self, request, obj, form, change):
if not obj.broker_promote:
obj.broker_promote = None
if not obj.broker_firstserve:
obj.broker_firstserve = None
super(UserProfileAdmin, self).save_model(request, obj, form, change)



Etienne Robillard

unread,
Feb 12, 2018, 4:23:37 AM2/12/18
to Weifeng Pan, django...@googlegroups.com

Hi Weifeng,

plz show us your urls.py...

Etienne

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/76957a17-da3e-40c4-ba2b-2de780f2da4e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Etienne Robillard
tka...@yandex.com
https://www.isotopesoftware.ca/

Julio Biason

unread,
Feb 12, 2018, 6:17:44 AM2/12/18
to django...@googlegroups.com
Hi Weifeng,

I think the problem is here:


django.urls.exceptions.NoReverseMatch: Reverse for 'nmm_tokenservice_userprofile_change' with arguments '('',)' not found. 1 pattern(s) tried: ['admin/nmm_tokenservice\\/userprofile\\/(?P<object_id>.+)\\/change\\/$']

By all the Django is saying, you're trying to reverse the URL for `mm_tokenservice_userprofile_change` passing an empty string as args; but your regex por it requres an `object_id` that must have at least 1 character (that's what `.+` means). So, because you're passing no parameters, it can't resolve the URL properly.

You *need* to pass the object_id, as per your URL specification, on your reverse.

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554
Reply all
Reply to author
Forward
0 new messages