1

I'm trying to build a custom user registration system but when creating the profile page it throws me that error. I've seen other similar problems in Github but those doesn't match the same problem as the one I'm facing. Any idea why {% url 'accounts:usuario:pk' %} throws this error?

Here is what I got

accounts/views.py

from django.views.generic.detail import DetailView
from django.shortcuts import get_object_or_404

...
class UserProfileView(DetailView):
    model = CustomUser
    template_name = 'registration/edit_profile.html'

    def get_context_data(self, *args, **kwargs):
        user = CustomUser.objects.all()
        context = super(UserProfileView, self).get_context_data(*args, **kwargs)

        usuario = get_object_or_404(CustomUser, id=self.kwargs['pk'])

        context['usuario'] = usuario
        return context
...

accounts/urls.py

from django.contrib import auth
from django.contrib.auth import views as auth_views
from django.urls import path

from .views import (
    ...
    UserProfileView,
)

app_name = 'accounts'

urlpatterns = [
    ...
    path('perfil/<int:pk>', UserProfileView.as_view(), name='usuario'),
    ...
]

projectname/urls.py

from django import contrib
from django import urls
from django.conf import settings
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from .views import (
    home_page
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls', namespace='accounts')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('', home_page, name='home'),
    ...
]

if settings.DEBUG:
    #test mode
    from django.conf.urls.static import static
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

templates/navbar.html

{% load static %}

<!-- Navigation-->
<nav class="navbar navbar-expand-lg navbar-light fixed-top py-3" id="mainNav">
        ...

            {% if user.is_authenticated %}
                    <div class="dropdown" id="profile">
                        <button type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fas fa-user"></i></button>
                        <ul class="dropdown-menu dashboard" aria-labelledby="dropdownMenuButton">
                            <li class="dropdown"></li>
                            <p>{{ user.username }} </p>
                            <li><a href="{% url 'accounts:usuario:pk' %}">Perfil</a></li>
                            <li><a href="{% url 'accounts:password_reset' %}">Cambiar contraseña</a></li>
                            <li><a class="btn btn-outline-primary login" href="{% url 'accounts:logout' %}" role="button">Logout</a></li>
                        </ul>
                    </div>
            {% else %}
                <a class="btn btn-outline-primary login" href="{% url 'accounts:login' %}" role="button">Login</a>
            {% endif %}

...
</nav>
Rodragon
  • 135
  • 3
  • 12
  • 1
    You have to add arguments like this: `{% url 'accounts:usuario' pk %}` – NKSM Apr 06 '21 at 19:32
  • Thanks. Fixed that.,but now I still got an error with the logic saying `Reverse for 'usuario' with arguments '('',)' not found. 1 pattern(s) tried: ['accounts/perfil/(?P[0-9]+)$']` – Rodragon Apr 06 '21 at 19:42

1 Answers1

1

You have to add arguments like this: {% url 'accounts:usuario' user.pk %}

Also your context does not have pk, it seems that you have to use user.pk

Look at template tag url for more info.

NKSM
  • 5,422
  • 4
  • 25
  • 38
  • Thanks. It worked. Do you know how could work using adding it to the context? – Rodragon Apr 06 '21 at 19:52
  • 1
    @RodDelaporte, It does not matter. "pk" is more independent. See this answer https://stackoverflow.com/a/2165880/6143954 – NKSM Apr 06 '21 at 19:54
  • 1
    @RodDelaporte, I suppose you are using this template as include, so I think it is better to use `user.pk` here, to avoid conflicts. – NKSM Apr 06 '21 at 20:01