[英]Django Form: Drop-Down/Select Not Rendering
我目前的問題是我有一個表單,Command_Form,它有兩個字段,host_id 和 current_commands。 Current_commands 應該是 CHOICES 選項的下拉菜單。 但是它並沒有這樣做,而是當我加載頁面時,它仍然是我可以輸入的文本字段,而不是下拉選擇菜單。
Forms.py 代碼
from django import forms
from django.forms import ChoiceField, ModelForm, RadioSelect
from .models import command_node
from .models import beacon
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'),
]
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=CHOICES
)
視圖.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 = Command_Form(request.POST)
if form.is_valid():
form.save()
return render(request, 'home.html', {"form": form})
return render(request, 'home.html', {"form": form},)
相關的 HTML 部分
/br>
</br>
<form action="" method=POST>
{% csrf_token %}
{{ form }}
<button type="Submit" class="btn btn-secondary btn-sm">Submit</button>
</form>
</body>
</html>
模型.py
from tkinter import CASCADE
from turtle import update
from django.db import models
class beacon(models.Model):
host_id = models.BigAutoField('Id', primary_key=True)
hostname = models.CharField('Hostname', max_length=200)
internalIp = models.CharField('Internal-IP', max_length=200)
externalIp = models.CharField('External-IP', max_length=200)
current_user = models.CharField('Current_User', max_length=200)
os = models.CharField('OS', max_length=200)
admin = models.CharField('Admin', max_length=200)
def __str__(self):
return self.hostname
class command_node(models.Model):
host_id = models.ForeignKey(beacon, on_delete=models.CASCADE)
current_commands = models.CharField('current_command', max_length=50, null=True)
previous_commands = models.CharField('previous_commands', max_length=2000, null=True)
def __str__(self):
return str(self.host_id)
您可以像在 forms.py 中顯示host_id
一樣顯示current_commands
但是,我建議通過以下方式直接在command_node
模型中設置選項:
使用當前代碼在 models.py 和 forms.py 中進行以下修改。
試試下面的代碼:
模型.py
class beacon(models.Model):
host_id = models.BigAutoField('Id', primary_key=True)
hostname = models.CharField('Hostname', max_length=200)
internalIp = models.CharField('Internal-IP', max_length=200)
externalIp = models.CharField('External-IP', max_length=200)
current_user = models.CharField('Current_User', max_length=200)
os = models.CharField('OS', max_length=200)
admin = models.CharField('Admin', max_length=200)
def __str__(self):
return self.hostname
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'),
]
class command_node(models.Model):
host_id = models.ForeignKey(beacon, on_delete=models.CASCADE)
current_commands = models.CharField(
choices=CHOICES, max_length=50, null=True)
previous_commands = models.CharField(
'previous_commands', max_length=2000, null=True)
def __str__(self):
return str(self.host_id)
表格.py
from django import forms
from django.forms import ChoiceField, ModelForm, RadioSelect, SelectMultiple
from .models import CHOICES, 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.SelectMultiple(
attrs={
'class': 'form-control'
},
)
)
Note:
django 中的模型、表單、基於類的視圖必須用PascalCase
而不是snake_case
編寫,因為它們是 python 的類,最好將模型分別命名為CommandNode
和Beacon
而不是和command_node
。 而beacon
CommandNodeForm
不是。Command_Node
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.