HTML 期中的 Django 渲染表单
Django Rendering Form in HTML Issue
我正在尝试在 html 中呈现一个表单,并尝试了普通的 {{ form }}。但是,当我访问由 python manage.py runserver 设置的站点时。我在表格应该去的地方得到以下信息(屏幕截图上突出显示的部分)
webpage
这是相关表单的代码。
forms.py
from django import forms
from django.forms import ChoiceField, ModelForm, RadioSelect
from .models import command_node
from .models import beacon
class Command_Form(ModelForm):
class Meta:
model = command_node
fields = (
'host_id',
'current_commands'
)
host_id = forms.ModelChoiceField(
required=True,
queryset=beacon.objects.all(),
widget=forms.Select(
attrs={
'class': 'form-control'
},
)
)
current_comamnds = forms.ChoiceField(
required=True,
choices=[
('Sleep', "Sleep"),
('Open SSH_Tunnel', 'Open SSH_Tunnel'),
('Close SSH_Tunnel', 'Close SSH_Tunnel'),
('Open TCP_Tunnel', 'Open TCP_Tunnel'),
('Close TCP_Tunnel', 'Close TCP_Tunnel'),
('Open Dynamic', 'Open Dynamic'),
('Close Dynamic', 'Close Dynamic'),
('Task', 'Task'),
])
这是有问题的 html。我删除了不相关的部分
home.html
</br>
</br>
<form action="" method=POST>
{% csrf_token %}
{{ form }}
<button type="Submit" class="btn btn-secondary btn-sm">Submit</button>
</form>
</body>
</html>
最后,我将这些观点包括在内,因为我怀疑它可能与此有关,但我不确定。我试图做到这一点,以便在提交表单时重新加载页面。
Views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import beacon
from .models import command_node
from .forms import Command_Form
from django.http import HttpResponseRedirect
def home(request):
form = Command_Form,
if request.method == "POST":
form = form(request.POST)
if form.is_valid():
form.save()
return render(request, 'home.html', {"form": form})
return render(request, 'home.html', {"form": form},)
尝试:
form = Command_Form()
if request.method == "POST":
form = Command_Form(request.POST)
我相信您没有实例化模型,这就是问题所在。
我正在尝试在 html 中呈现一个表单,并尝试了普通的 {{ form }}。但是,当我访问由 python manage.py runserver 设置的站点时。我在表格应该去的地方得到以下信息(屏幕截图上突出显示的部分)
webpage
这是相关表单的代码。
forms.py
from django import forms
from django.forms import ChoiceField, ModelForm, RadioSelect
from .models import command_node
from .models import beacon
class Command_Form(ModelForm):
class Meta:
model = command_node
fields = (
'host_id',
'current_commands'
)
host_id = forms.ModelChoiceField(
required=True,
queryset=beacon.objects.all(),
widget=forms.Select(
attrs={
'class': 'form-control'
},
)
)
current_comamnds = forms.ChoiceField(
required=True,
choices=[
('Sleep', "Sleep"),
('Open SSH_Tunnel', 'Open SSH_Tunnel'),
('Close SSH_Tunnel', 'Close SSH_Tunnel'),
('Open TCP_Tunnel', 'Open TCP_Tunnel'),
('Close TCP_Tunnel', 'Close TCP_Tunnel'),
('Open Dynamic', 'Open Dynamic'),
('Close Dynamic', 'Close Dynamic'),
('Task', 'Task'),
])
这是有问题的 html。我删除了不相关的部分
home.html
</br>
</br>
<form action="" method=POST>
{% csrf_token %}
{{ form }}
<button type="Submit" class="btn btn-secondary btn-sm">Submit</button>
</form>
</body>
</html>
最后,我将这些观点包括在内,因为我怀疑它可能与此有关,但我不确定。我试图做到这一点,以便在提交表单时重新加载页面。
Views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import beacon
from .models import command_node
from .forms import Command_Form
from django.http import HttpResponseRedirect
def home(request):
form = Command_Form,
if request.method == "POST":
form = form(request.POST)
if form.is_valid():
form.save()
return render(request, 'home.html', {"form": form})
return render(request, 'home.html', {"form": form},)
尝试:
form = Command_Form()
if request.method == "POST":
form = Command_Form(request.POST)
我相信您没有实例化模型,这就是问题所在。