hovel / imagestore

Django gallery solution.
BSD 3-Clause "New" or "Revised" License
164 stars 72 forks source link

Custom users not found #56

Open sandino opened 8 years ago

sandino commented 8 years ago

Hi,

I have a custom user model CustomUser. On album list page under each album cover I put album user name with a link to {% url 'imagestore:user' album.user.username %}, and the url resolves correctly. But when I click the link I open Http404 page with No CustomUser matches the given query message. Using pdb debugger I found that in this line the specified user cannot be found as it is being looked for using django.contrib.auth.models.User while it is an instance of the custom model.

lampslave commented 8 years ago

Thank you.

sandino commented 8 years ago

I got that finally. The problem was that I passed username as an argument while my username_field is email. But if I put {% url 'imagestore:user' album.user.email %} into template I get NoReverseMatch. To fix it we need to change the url config something like this

-url(r'^user/(?P<username>\w+)/albums/', AlbumListView.as_view(), name='user'),
+url(r'^user/(?P<username>[@\.\w]+)/albums/', AlbumListView.as_view(), name='user'),

so it can resolve url with email address in it. Same to the next line:

-url(r'^user/(?P<username>\w+)/$', ImageListView.as_view(), name='user-images'),
+url(r'^user/(?P<username>[@\.\w]+)/$', ImageListView.as_view(), name='user-images'),
lampslave commented 8 years ago

Hm, I don't think it's a good idea. \w+@\w+(?:\.\w+)+ is a little hard to understand. Maybe you want to try to override this pattern for your project only?

sandino commented 8 years ago

See I changed it to [@\.\w]+ in my previous comment.

I've already overridden the pattern for my project.

But I think it would be nice for imagestore to allow email in url config since it uses username_field variable, not just username explicitly.

lampslave commented 8 years ago

OK, thank you, we (@zeus, @GeyseR) will think about it.

sandino commented 8 years ago

I had few issues with url regex this night. Now I decided to use [\w|\W]+ that covers all cases.