0

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!

cjuggler
  • 27
  • 6
  • Does this answer your question? [Override label in Django Forms](https://stackoverflow.com/questions/42467540/override-label-in-django-forms) – John S Nov 14 '21 at 16:20
  • Hi John S, Thank you for your comment. Unfortunately I'm still new to django (and programming in general) and I'm not entirely sure I understand the mail thread enough to know if it is related. – cjuggler Nov 15 '21 at 15:06
  • Hi Abdul, thank you. I do not think so, it seems the thread is about modifying widget attributes but I only want to reference a variable within a view definition. – cjuggler Nov 16 '21 at 16:30
  • 1
    @cjuggler but you _do_ want to do that, you want to modify the widget's attribute with something from the view. You can't do that _directly_ (variable scope) like you do, you need to use the solutions from the linked question and pass the value to your form from the view like `EditPageForm(topic=topic)`. – Abdul Aziz Barkat Nov 17 '21 at 05:30
  • Hi Abdul, I see now. I thought it might be doable directly but it seems otherwise. Thank you! – cjuggler Nov 19 '21 at 10:49

1 Answers1

-1

In my opinion, you should extend class parameter by topic variable.

like:

class EditPageForm(forms.Form, topic):
    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), "topic":f"{topic}"
    })
azarjaved
  • 63
  • 5
  • Hi azarjaved, Thank you for your response. Unfortunately that does not work as well. I still get an error that 'topic' is not defined. Welcome any thoughts you might have on the same. – cjuggler Nov 15 '21 at 15:04