% uuid, ProfileDetailView.as_view(), name="profile_deta...">

Go uuid to reverse () to create url

I have url:

url(r'^profile_detail/(?P<uuid>%s)/$' % uuid, ProfileDetailView.as_view(), name="profile_detail_view")

And I need to redirect the user to this view, but I do not know how to create a url without hard coding, which I do not want to do.

I understood something like:

reverse('profile_detail_view' 'profile.uuid')

And I tried some variations of this, but I did not understand. I also tried something with args and kwargs, but nothing.

How should I do it?

+4
source share
1 answer

urls.py
In urls.pyyou define only templates that, when agreed upon, invoke this view. What was matched will be passed to the view as a named argument, for example.

url(
    r'^profile_detail/(?P<uuid>[\d\-]+)/$', 
    ProfileDetailView.as_view(), 
    name="profile_detail_view"
)

(0-9) (-) ( uuid ).


, uuid ( , ),

class ProfileDetailView(View):
    def get(self, request, uuid):
        try:
            user = User.objects.get(uuid=uuid)
        except User.DoesNotExist:
            raise Http404  # or whatever else is appropriate

        # ...


, reverse. kwargs URL (?P<uuid>[\d\-]+)

reverse('profile_detail_view', kwargs={'uuid': profile.uuid})
+7

Source: https://habr.com/ru/post/1584332/


All Articles