Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

385
Vistas
Django get_absolute_url calling same URL as current page

I am trying to add a "+ New Review" link to a page on my Django site that will take you to a 'uuid:pk/new_review/' page that houses a form to submit a URL for a specific post. I am currently facing issues where the link is redirecting to the same page, and I think the issue is stemming from my use of get_absolute_url(), which I am still having trouble getting my head around.

urls.py

urlpatterns = [
    path('<uuid:pk>/', ListingDetailView.as_view(), name = 'listing_detail'),
    path('<uuid:pk>/new_review/', NewReview.as_view(), name = 'new_review'),
]

views.py

class ListingDetailView(LoginRequiredMixin, DetailView):
    
    model = Listing
    template_name = 'listing/listing_detail.html'
    context_object_name = 'listing_detail'
    login_url = 'account_login'

class NewReview(LoginRequiredMixin, CreateView):

    model = Review
    template_name = 'listing/new_review.html'
    fields = ['review']
    context_object_name = 'new_review'

    def form_valid(self, form):
        form.instance.user = self.request.user
        form.instance.listing_id = get_object_or_404(Listing, pk = self.kwargs['pk'])
        return super().form_valid(form)

models.py

class Review(models.Model):

    listing_id = models.ForeignKey(
        Listing,
        on_delete=models.CASCADE,
        related_name='reviews',
    )

    user = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )

    review = models.TextField(max_length=5000)

    def __str__(self):
        return self.listing_id

    def get_absolute_url(self):
        return reverse('listing_detail', args=[str(self.listing_id)])

listing_detail.html

...
    <p><a href="{{ new_review.get_absolute_url }}">+ New Review</a></p2>
...
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The issue is not get_absolute_url itself: that will return an empty string, since there is no new_review in the context.

You should work with:

<a href="{% url 'new_review' pk=listing_detail.pk %}">+ New Review</a>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda