0

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.

2
  • Judging by the url you have provided, it doesn't look like you can redirect to the same view without the parameter (since it is compulsory). Commented Feb 13, 2018 at 14:22
  • Possible duplicate of Django optional url parameters Commented Feb 13, 2018 at 14:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly i am doing that, but i have alot of models that need crud, and i cannot enter all the views one by one in : return redirect('board_redirect')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.