'Why is the Django template file not updating value from the context?
(I am a beginner in Django)
I am following along a tutorial in Django where we can dynamically pass some variable to a template file from associated function in views.py
When I am running the code for the first time, it works fine, but when I change the values within the context, the template does not update with the new values even when I refresh it
Here is my views.py code -
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# return HttpResponse('<h1>Hey, welcome</h1>')
# we can dynamically send data to our index.html file as follows -
context={
'u_name': 'Ankit',
'u_age': 25,
'u_nationality': 'Indian',
}
return render(request,'index.html',context)
Here is my template index.html -
<h1>
How are you doing today {{u_name}} <br>You are {{u_age}} years old<br>Your nationality is {{u_nationality}}
</h1>
settings.py has been correctly set as well -
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR,'templates'],
Editing post to add urls.py as well (this is within the app)
from . import views
from django.urls import include,path
urlpatterns = [
path('',views.index,name='index')
]
And the urls.py in the main project -
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('MyApp.urls'))
]
Here is my output - does not take the values from the context
Would be glad if someone could point out what error I am making. Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
