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.

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
Amaan Khan
  • 181
  • 2
  • 3
  • 15
  • 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). – shad0w_wa1k3r Feb 13 '18 at 14:22
  • Possible duplicate of [Django optional url parameters](https://stackoverflow.com/questions/14351048/django-optional-url-parameters) – joppich Feb 13 '18 at 14:27

1 Answers1

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.

Vitor Freitas
  • 3,550
  • 1
  • 24
  • 35
  • 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') – Amaan Khan Feb 13 '18 at 14:35