英文:
DJANGO: the combobox doesn't print in the textarea. Error in in html?
问题
在home.html
页面中,我有一个combobox
和一个textarea
。我想要实现的是,如果我点击combobox中的某个项目,在views.py
中使用条件语句,我希望在textarea中打印出相应的内容。例如,如果我选择"Red",它应该打印出"You chose red"。非常简单。但是我的代码似乎有问题。问题可能出现在home.html
文件或views.py
文件中的def home
函数中(我认为是这里)。
问题: combobox(独立于数据库且未附加)可以正确浏览列表中的项目,但条件不适用。当我点击按钮时,没有任何反应。
请问如何正确执行条件并在选择combobox项目时在textarea中打印出内容?我是Django的新手,能否请你用代码示例给我一个答案?
请提供home.html
、views.py
和forms.py
的代码。我希望有人能帮助我。我认为问题出在HTML中!谢谢!
home.html
<form method="post">
{% csrf_token %}
{{ combobox }}
<button type="submit">submit</button>
</form>
<form method="post">
{% csrf_token %}
{{ textarea }}
</form>
views.py
colours = ["Red", "Blue", "Black", "Orange"]
@login_required
def home(request):
# Combobox
if request.method == "POST":
combobox = SimpleCombobox(request.POST)
if combobox.is_valid():
print(combobox.cleaned_data)
return redirect("home")
else:
combobox = SimpleCombobox()
# TextArea
if request.method == "POST":
textarea = SimpleTextbox(request.POST)
if textarea.is_valid():
print(textarea.cleaned_data)
return redirect("home")
else:
textarea = SimpleTextbox()
message = ""
picked = ""
if request.method == 'POST':
picked = request.POST['cities']
if picked == 'Red':
message = "<<< You chose red"
elif picked == 'Blue':
message = "<<< You chose blue"
elif picked == 'Black':
message = "<<< You chose black"
else:
message = "<<< Oh no, you chose orange"
return render(request, 'app1/home.html', {"combobox": combobox, "textarea": textarea, "colours": colours, "chosen": picked, "message": message})
forms.py
from django import forms
class SimpleCombobox(forms.Form):
COLOR_CHOICES = (
("Red", "Red"),
("Blue", "Blue"),
("Black", "Black"),
("Orange", "Orange"),
)
cities = forms.ChoiceField(choices=COLOR_CHOICES)
class SimpleTextbox(forms.Form):
coverletter = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 4, 'cols': 40}))
# LOGIN
class LoginForm(forms.Form):
username = forms.CharField(max_length=65)
password = forms.CharField(max_length=65, widget=forms.PasswordInput)
请注意,我在代码中进行了一些修改以解决问题,特别是在处理POST请求时。希望这对你有帮助。
英文:
In the home.html
page, i have a combobox
and a textarea
. I would like to achieve that if i click on an item in the combobox, using an (if) condition in views.py
, i get the printout in the textarea. For example if I select "Red" then it must print "You chose red". Very simple. But there is something wrong with my code. Probably the problem is in the home.html
file or in the def home
function in the views.py
file (i believe).
Problem: combobox(independent and NOT attached to a database) correctly browses items from list, but the condition doesn't apply. When I click on button, nothing happens.
How can I execute the condition correctly and print in the textarea when I select the combobox item? I'm new to django, can you show me an answer with the code please?
Post the code of home.html
, views.py
and forms.py
. I hope someone can help me please. I think the problem is in the html! Thank you!
home.html
<form method="post">
{% csrf_token %}
{{ combobox }}
<button type="submit">submit</button>
</form>
<form method="post">
{% csrf_token %}
{{ textarea }}
</form>
views.py
colours = ["Red", "Blue", "Black", "Orange"]
@login_required
def home(request):
#Combobox
if request.method == "POST":
combobox = SimpleCombobox(request.POST)
if combobox.is_valid():
print(combobox.cleaned_data)
return redirect("home")
else:
combobox = SimpleCombobox()
#TextArea
if request.method == "POST":
textarea = SimpleTextbox(request.POST)
if textarea.is_valid():
print(textarea.cleaned_data)
return redirect("home")
else:
textarea = SimpleTextbox()
message = ""
picked = ""
if request.method == 'POST':
picked = request.form['colours']
if picked == 'Red':
message = "<<< You chose red"
elif picked == 'Blue':
message = "<<< You chose blue"
elif picked == 'Black':
message = "<<< You chose black"
else:
message = "<<< Oh no, you chose orange"
return render(request, 'app1/home.html', {"combobox": combobox, "textarea": textarea, "colours": colours, "chosen": picked, "message": message})
forms.py
from django import forms
class SimpleCombobox(forms.Form):
Col1 = 'Red'
Col2 = 'Blue'
Col3 = 'Black'
Col4 = 'Orange'
COLOR_CHOICES = (
(Col1, u"Red"),
(Col2, u"Blue"),
(Col3, u"Black"),
(Col4, u"Orange"),
)
cities = forms.ChoiceField(choices=COLOR_CHOICES)
class SimpleTextbox(forms.Form):
coverletter = forms.CharField(required=False,
widget=forms.Textarea(
# rows and colums of the textarea
attrs={'rows': 4, 'cols': 40}))
#LOGIN
class LoginForm(forms.Form):
username = forms.CharField(max_length=65)
password = forms.CharField(max_length=65, widget=forms.PasswordInput)
UPDATE ONLY FOR INFO
For info, if it could help for the answer, I previously use this code html
on Flask
and it worked fine (I use the same combobox, textarea and condition but with Flask and it worked fine)
<form name="Item_1" action="/user/home" method="POST">
<select name="colours">
{% for colour in colours %}
{% if colour == chosen %}
<option value="{{ colour }}" SELECTED>{{ colour }}</option>
{% else %}
<option value="{{ colour }}">{{ colour }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit">
</form>
<form action = "/result" method = "POST">
<input type="text" name="result" value="{{ message }}" />
</form>
答案1
得分: 1
#views.py
def DemoView(request):
color_choices = ("红色", "蓝色", "黑色", "橙色")
message = ''
if request.method == "POST":
picked = request.POST.get('color')
if picked == '红色':
message = '<<< 你选择了红色'
print(message)
elif picked == '蓝色':
message = '<<< 你选择了蓝色'
elif picked == '黑色':
message = '<<< 你选择了黑色'
else:
message = '<<< 哦,不好,你选择了橙色'
context = {'message': message, 'color_choices': color_choices}
return render(request, 'index.html', context)
HTML code
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div class="container p-5">
<div class="row mx-auto">
<div class="col-6">
<form action="" method="POST" novalidate class="form-group">
{% csrf_token %}
<select name="color" class="form-select">
{% for i in color_choices %}
<option value="{{i}}">{{i}}</option>
{% endfor %}
</select>
<textarea class="form-control" name="msg" cols="30" rows="10">{{message}}</textarea>
<button class="btn btn-primary mt-3" type="submit">提交</button>
</form>
</div>
</div>
</div>
<!-- end snippet -->
Output
英文:
#views.py
def DemoView(request):
color_choices = ("Red","Blue","Black","Orange")
message =''
if request.method =="POST":
picked = request.POST.get('color')
if picked == 'Red':
message = "<<< You chose red"
print(message)
elif picked == 'Blue':
message = "<<< You chose blue"
elif picked == 'Black':
message = "<<< You chose black"
else:
message = "<<< Oh no, you chose orange"
context = {'message':message,'color_choices':color_choices}
return render(request, 'index.html', context)
HTML code
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div class="container p-5">
<div class="row mx-auto">
<div class="col-6">
<form action="" method="POST" novalidate class="form-group">
{% csrf_token %}
<select name="color" class="form-select" >
{% for i in color_choices %}
<option value="{{i}}">{{i}}</option>
{% endfor %}
</select>
<textarea class="form-control" name="msg" cols="30" rows="10">{{message}}</textarea>
<button class="btn btn-primary mt-3" type="submit">Submit</button>
</form>
</div>
</div>
</div>
<!-- end snippet -->
Output
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论