简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM