+ 1
Really need help with Django!!
Hi, I am having a problem with my Django code. As this website does not provide anything on Django I have submitted my question on stackoverflow at the following link : https://stackoverflow.com/questions/62542089/how-to-use-django-button-as-form-input-field Hope someone can help. Thank you
4 Réponses
+ 2
Hi, you could use a hidden input.
#forms.py
class fontform(forms.ModelForm):
btn = forms.IntegerField(widget=forms.HiddenInput(attrs={'id' : 'myfieldid' })
class Meta:
model = layout
fields = ['btn']
#template
...
<form method='POST' action='{% url 'testview' %}'>
{{ form }}
<span id="preview">Text Size</span>
<button onclick='document.getElementById("myfieldid").value = parseInt(document.getElementById("myfieldid").value)+1;document.getElementById("preview").style.fontSize = document.getElementById("myfieldid").value + "px"'>
<button onclick='document.getElementById("myfieldid").value = parseInt(document.getElementById("myfieldid").value)-1;document.getElementById("preview").style.fontSize = document.getElementById("myfieldid").value + "px"'>
<input type="submit">
</form>
...
+ 1
#views.py
def test_view(request):
if request.method == 'POST':
form = fontform(request.POST)
if form.is_valid():
form.save()
else:
form = fontform() return
render(request, 'home.html')
+ 1
Or perhaps you could use a range input?