I'm learning Django and trying to reference a variable from def in views as a placeholder. In the code below, 'Replace this' should show the content of 'topic' as defined in 'def editpage(request, topic)' but I cannot seem to get the reference to work.
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django import forms
from . import util
class EditPageForm(forms.Form):
title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Replace this', 'class': 'form-control'}))
def editpage(request, topic):
return render(request, "encyclopedia/editpage.html", {"form": EditPageForm(), "topic":f"{topic}"
})
My attempt was to swap 'Replace this' with f'{topic}', but it does not appear to work.
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django import forms
from . import util
class EditPageForm(forms.Form):
title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': f'{topic}', 'class': 'form-control'}))
def editpage(request, topic):
return render(request, "encyclopedia/editpage.html", {"form": EditPageForm(), "topic":f"{topic}"
})
Instead, I get an error that 'topic' is not defined. Thanks in advance!