简体   繁体   中英

Adding/Removing url patterns from django during runtime

For example I have a url in my urls.py-

url(r'^my-url/$', views.my_view)

Now on a particular action from views, I want to remove this url from urlpatterns and add a new url during runtime. Is it possible, if so, how?

Changing url handler during runtime is not best practice instead you could have the checkpoint in db and handle all the incoming request

models.py

class Version(models.Model):
    version_number = models.IntegerField()
    is_latest = models.BooleanField(default=False)

urls.py

url(r'^handler/(?P<version>\d+)/$', views.handler)

views.py

from django.shortcuts import get_object_or_404

def handler(request, version):
    obj = get_object_or_404(Version, version_number=4)
    if obj.is_latest:
        return render(request,'base.html')
    else:
        return render(request, 'old.html')

Above code authorise only version 4 ( /handler/4 )

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