Django development server seems to be using old version of python source file
Question: I'm re-writing the code of my website, and testing with Django's built-in web
server using the `manage.py runserver` command. Now I've come across a very
strange problem: The server seems to use the current version of `views.py` on
the very first page load, but all subsequent refreshes give me a server error
because the server is apperently using an older version of `views.py`, but the
current versions of all other files, which leads to errors – specifically URL
resolver errors, because I changed some code from using hard-coded paths in
`views.py` to using the URL resolver, which of course doesn't work if the URL
resolver receives a _path_ (from the old `views.py`) when it's expecting a
view name (which I put in the new `views.py`).

I have already deleted all the `.pyc` files in my django project directory and
rebooted the machine, to no avail. The problem persists.

I'm using Django 1.7.6 on Python 3.4.2.

Here's the current `views.py` (it doesn't really make sense, it's just for
testing):

    
    
    from mezgrman.utils import NavigationTemplateResponse
    
    NAV_DATA = {
        'app_root': 'index',
        'app_title': "Item Manager",
        'navbar': [
            ("Add Item", 'index'),
        ],
        'page_title': "Item Manager",
    }
    
    def index(request):
        return NavigationTemplateResponse(request, "design_test/index.html", NAV_DATA)
    

The `NavigationTemplateResponse` is a subclass of `TemplateResponse`:

    
    
    from django.template.response import TemplateResponse
    from django.core.urlresolvers import resolve, reverse
    
    class NavigationTemplateResponse(TemplateResponse):
        def __init__(self, request, template, nav_data, context = None, content_type = None, status = None, current_app = None):
            if context is None:
                context = {}
    
            url_name = resolve(request.path).url_name
            app_name = url_name.split(".")[0]
            view_prefix = app_name + ".views."
    
            nav_data['app_root'] = reverse(view_prefix + nav_data.get('app_root', ""))
    
            for index, entry in enumerate(nav_data.get('navbar', [])):
                title, view_name = entry
                nav_data['navbar'][index] = (title, reverse(view_prefix + view_name))
    
            context.update(nav_data)
            return super().__init__(request, template, context, content_type, status, current_app)
    

The Django server traceback explicitly proves that it's using an old version
of `views.py`, these are the local variables (sans the `WSGIRequest`) at the
time of the error, where `nav_data` is the same as in the old `views.py`:

    
    
    content_type        None
    template            'design_test/index.html'
    url_name            'design_test.views.index'
    status              None
    self                <mezgrman.utils.NavigationTemplateResponse object at 0x7f395f8d15f8>
    app_name            'design_test'
    __class__           <class 'mezgrman.utils.NavigationTemplateResponse'>
    context             {}
    view_prefix         'design_test.views.'
    current_app         None
    nav_data            {
                            'app_root': '/',
                            'app_title': 'Item Manager',
                            'navbar': [('Add Item', '/')],
                            'page_title': 'Item Manager'
                        }
    

This seems to me like a bug in Django, but I'd like to know if there's another
reason for this strange behaviour. Any help would be appreciated.


Answer: It's not bug, and also this is not old code of `views.py`. You're simply
overwriting your data in NAV_DATA inside your view. In first request from
starting server NAV_DATA have it's initial values, but on that request you're
overriding some values with reversed urls. That change persists between
requests until dev server is reloaded.

**Solution 1:** work on copy of your dict:

    
    
    class NavigationTemplateResponse(TemplateResponse):
        def __init__(self, request, template, nav_data, context = None, content_type = None, status = None, current_app = None):
            nav_data = nav_data.copy()
    

**Solution 2:** change your logic to store reversed urls in other variables

**Solution 3:** change your logic to behave differently when urls are already
reversed. That solution is **not** thread safe!

