Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - AttributeError: 'UserProfile' object has no attribute 'urls'

I'm trying to extend User in Django. This new User should have more attributes than built-in User and some of them should be required during registration. Moreover, I would like to see this new type of user in Admin. I've followed official tutorial and this youtube video but there is an error.

I'm new in Django so I can't find out where the problem could be. Do you know whats wrong?

EDIT: I've tried to stop server and makemigrations - the same error occured.

CMD:

  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\venv\
lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\venv\
lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\venv\
lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\Solut
ionsForLanguagesProject\urls.py", line 22, in <module>
    url(r'^admin/', admin.site.urls),
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\venv\
lib\site-packages\django\contrib\admin\sites.py", line 303, in urls
    return self.get_urls(), 'admin', self.name
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages\venv\
lib\site-packages\django\contrib\admin\sites.py", line 287, in get_urls
    url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(mo
del_admin.urls)),
AttributeError: 'UserProfile' object has no attribute 'urls'

MODELS.PY:

...
from django.db.models.signals import post_save
...
class UserProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    first_name = models.CharField(max_length=40)
    surname = models.CharField(max_length=40)
    email = models.EmailField()
    telephone = models.CharField(max_length=40)

    def __unicode__(self):
        return '{} {}'.format(self.first_name,self.surname)

def create_profile_user_callback(sender,instance, **kwargs):
    profile, new = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_profile_user_callback, User)

ADMIN.PY:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from models import *
from .models import UserProfile

admin.site.register(AdminContact)

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'User_Profile'

class UserAdmin(BaseUserAdmin):
    inlines = (UserProfileInline, )

admin.site.unregister(User)
admin.site.register(User,UserProfile)

URLS.PY:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^about', TemplateView.as_view(template_name='static/about-us.html')),
    url(r'^terms-and-conditions', TemplateView.as_view(template_name='static/terms-and-conditions.html')),
    url(r'^faq', TemplateView.as_view(template_name='static/faq.html')),
    url(r'^$', views.index),
    url(r'^contact-us', views.contact_us),
]

registration_patterns = [
    url(r'register', views.register),
    url(r'register_success', views.register_success),
]

urlpatterns += registration_patterns
like image 227
Milano Avatar asked Feb 07 '23 09:02

Milano


1 Answers

In admin.py you are registering the UserProfile model as the admin for User; I suspect you meant to register the modeladmin:

admin.site.register(User,UserAdmin)

(Note though that the default User already includes first_name, last_name and email, so I don't know why you've duplicated them in your UserProfile model.)

like image 164
Daniel Roseman Avatar answered Feb 12 '23 10:02

Daniel Roseman