0

I need to change my urls in site without reloading server.

Using: Django 4.0.2

with open('settings.json', 'r+') as f:
    data = json.load(f)
    if data['urls']['posts']["type"] != 'Custom':
        if data['urls']['posts']["type"] == 'Name':
            urlpatterns.append(path('<slug:slug>/', details))
        if data['urls']['posts']["type"] == 'Id':
            urlpatterns.append(path('<int:pk>/', details))

So if param type change, django don't see that, cause urls.py runs only on server reload

Probably we can make server reload, but for sure not manualy So i used this:

import sys
from django.conf import settings


def reload_urlconf(urlconf=None):
    if urlconf is None:
        urlconf = settings.ROOT_URLCONF
    if urlconf in sys.modules:
        reload(sys.modules[urlconf])

I had an error:

Unresolved reference 'reload'

Chipnick
  • 43
  • 1
  • 1
  • 6
  • 1
    Why without restarting the server? What are you trying to do with your URLs? This is almost certainly an [XY problem](https://meta.stackexchange.com/q/66377/248627). – ChrisGPT was on strike Mar 11 '22 at 12:29
  • 1
    you cannot change the URL while app is running without restarting the server – Zilay Mar 11 '22 at 12:30
  • It makes no sense that you can not reload the server. One of the key-aspects of a webserver is that it *should* be able to reload, that is why it makes use of a database for example to store data, and not memory. – Willem Van Onsem Mar 11 '22 at 12:34
  • so urls runs only one time, so if **type** changed urls wont't see that changes – Chipnick Mar 11 '22 at 12:45
  • can I somehow restart server in views? – Chipnick Mar 11 '22 at 12:48
  • 1
    Where is `type` stored and and how/why does it change? Seems like you could accept int or slug in your param and move this logic to your view? – Iain Shelvington Mar 11 '22 at 12:49
  • `type` is stored in _settings.json_, and changed in views, I supose I couldn't move logic to views cause i have different params, like `id` and `slug` – Chipnick Mar 11 '22 at 13:09
  • https://stackoverflow.com/questions/12865916/reload-django-urls-without-restarting-the-server check this – kjaw Mar 11 '22 at 13:21
  • What problem are you trying to solve here? If you are changing the files when running locally with `runserver`, django already autoreloads on changes. https://docs.djangoproject.com/en/4.0/ref/django-admin/ – sytech Mar 12 '22 at 04:07

0 Answers0