Amaan Khan
Amaan Khan

Reputation: 181

Get Current Url Without Parameters in Django

I have a url like below:

url(r'^board/(?P<pk>\d+)$', board_crud, name='board_update'),

I want to get current view 'board' without parameters so that i can redirect to it.

I want to redirect to current view(without param) in same view(with param).

Thanks in Advance.

Upvotes: 0

Views: 1992

Answers (1)

Vitor Freitas
Vitor Freitas

Reputation: 3610

I believe you want to do something like this:

urls.py

url(r'^board/$', board_redirect, name='board_redirect'),
url(r'^board/(?P<pk>\d+)/$', board_crud, name='board_update'),

PS: Note the ending /, it's a good idea to always end the url patterns with a forward slash, for consistency (except cases where you are return a url like sitemap.xml for example).

Then, you would need to create a view like this:

views.py

from django.shortcuts import redirect
from .models import Foo

def board_redirect(request):
    latest = Foo.objects.values('pk').order_by('-date').first()
    return redirect('board_update', pk=latest['pk'])

The queryset would define the logic you want to implement. I don't have more info on your application. In this example you would always show the "latest" object based on a "date" field. Hope it makes sense.

Upvotes: 1

Related Questions