我正在使用反射来读取xml文件并不断遇到错误告诉我我无法将字符串转换为字符串[](我不想这样做!)我认为我的问题的原因是我无法判断对象的类型是否为数组.以下是我目前正在使用的(不能正常工作),但我也尝试使用if(mi[i].GetType() == typeof(string[]))哪些也不起作用..
MemberInfo[] mi = objType.GetProperties();
for (int i = 0; i < mi.Length; i++)
{
if (mi[i].GetType().IsArray)
{
}
else
{
//Code path is running through here
}
Run Code Online (Sandbox Code Playgroud)
正确读入文件..
编辑:我想我最好把结构添加到我objType的更好的解释..
objType 是一个包含string []变量的类,在本例中称为 mi[i]
我正在使用后台工作人员来处理文件的加载以阻止我的 ui 冻结,但是似乎RunWorkerCompleted在我的DoWork事件完成之前就完成了(退出对话框时导致错误)......我做错了什么? 我最好在任务上做这个吗?
public static <T> LoadDesign(string xmlPath)
{
PleaseWait pw = new PleaseWait(xmlPath);
pw.ShowDialog();
return pw.design;
}
private PleaseWait(string xmlFile)
{
InitializeComponent();
bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += (s, e) =>
{
design = (Cast)DllCall containing XmlSerializer.Deserialize(...,xmlFile);
};
bw.RunWorkerCompleted += (s, e) => {
//Exit please wait dialog
this.Close();
};
if (!bw.IsBusy)
bw.RunWorkerAsync();
}
Run Code Online (Sandbox Code Playgroud)
我相信这个问题可能归结为我的后台工作人员正在调用一个 dll 而不是等待响应的事实。我试图添加检查,如while(design == null)无济于事..
Edit2 错误是 NRE,因为设计尚未加载,我可以轻松解决此问题,但宁愿让线程工作。
我正在尝试创建一个项目管理类型的应用程序。现在我有了如图所示的项目模型

当我保存它时,我可以获得用户名和时间。
现在。在下面的同一页面中,我想显示用户可以添加、编辑的表格。就像这张图片中一样
我怎样才能做到这一点?
我们要创建数据库表并在这里调用吗?我在这篇文章中找不到任何线索。
我的模型中有这个:
class Product(models.Model):
name = models.CharField(max_length=50)
category = models.ForeignKey(Categoria)
price = models.DecimalField()
def __str__(self):
return self.name
def dollar_price(self, dollar_price):
return self.price * dollar_price
Run Code Online (Sandbox Code Playgroud)
我想在视图中获取每个产品的美元价格:
def products(request):
p = Product.objects.all()
dollar = 10
for product in p:
dollar_price = product.dollar_price(dollar)
p[product].new_field = dollar_price # This line is the problem
return render(request, "main/products.html", {"p":p})
Run Code Online (Sandbox Code Playgroud)
在我发表评论的那一行,我知道我不能这样做,但我想创建一个“p”对象的新字段并用“dollar_price”填充它。
我怎样才能做类似的事情?
我从Android应用程序发送POST请求到我的服务器,但我收到此错误:
POST类似于: http:// example/my_page_url/1000 其中1000是ID.
这是我的观点方法:
def inventory(request, cross_id):
text_file = open("test.txt", "w")
text_file.write('POST Received')
text_file.write(cross_id.__str__())
text_file.close()
return render(request, 'Inventory.html', {})
Run Code Online (Sandbox Code Playgroud)
我的模板代码:
<form action='' method="POST">
<button type="submit" id="btn_save" name="btn_save">Save</button>
{% csrf_token %}
</form>
Run Code Online (Sandbox Code Playgroud)
实际上,我并不需要调用模板,因为我只想在服务器上执行某些操作.但我现在调用模板只是为了防止任何错误.
我已经阅读了相同问题的其他答案,但是他们都错过了模板中的CSRF令牌或者其他方法,但我相信这里的情况有所不同.
我有一个 python 脚本来发送电子邮件,它工作得很好,但问题是当我检查我的电子邮件收件箱时。

我希望该用户名是自定义用户名而不是整个电子邮件地址。

我尝试了几乎所有命令来杀死 gunicorn 服务器。但没有任何效果。我怎样才能杀死这些进程并释放 80 端口
12652 ? Ss 0:00 sudo gunicorn -b 0.0.0.0:80 foss.wsgi:application
12654 ? S 0:00 /usr/bin/python /usr/local/bin/gunicorn -b 0.0.0.0:80 foss.wsgi:application
12659 ? S 0:00 /usr/bin/python /usr/local/bin/gunicorn -b 0.0.0.0:80 foss.wsgi:application
12709 ? Ss 0:00 sudo gunicorn -b 0.0.0.0:80 foss.wsgi:application
12711 ? S 0:00 /usr/bin/python /usr/local/bin/gunicorn -b 0.0.0.0:80 foss.wsgi:application
12717 pts/11 S+ 0:00 grep --color=auto gunicorn
Run Code Online (Sandbox Code Playgroud) HTML
<ul class="dropdown-menu" role="menu">
<li>java <input type="checkbox" name="categories[]" value="Java"></li>
<li class="divider"></li>
<li>c <input type="checkbox" name="categories[]" value="C"></li>
<li class="divider"></li>
<li>network <input type="checkbox" name="categories[]" value="Network"></li>
<li class="divider"></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
蟒蛇
list_categories = request.POST.getlist['categories']
Run Code Online (Sandbox Code Playgroud)
此代码导致错误
'instancemethod'对象没有属性
'__getitem__'.
我已经尝试过了
list.categories = request.POST['categories']
Run Code Online (Sandbox Code Playgroud) 如果我有一个可以有多种类型的 django 模型(假设它们很少改变,但可能会添加新的类型),那么使用 int 更好吗?
from enum import Enum
class Places(Enum):
gym = 0
coffee_shop = 1
home = 2
garage = 3
office = 4
study = 5
shed = 6
Run Code Online (Sandbox Code Playgroud)
或者将其作为字符串更好?
GYM = 'gym'
COFFEE_SHOP = 'coffee_shop'
HOME = 'home'
GARAGE = 'garage'
OFFICE = 'office'
STUDY = 'study'
SHED = 'shed'
Run Code Online (Sandbox Code Playgroud)
我喜欢字符串,因为它们是人类可读的(文档似乎使用 FR、JR、SR 等字符串),但我经常看到人们使用整数来执行此操作。
有人可以解释为什么吗?
我正在尝试创建一个类的实例,我可以通用方式添加到列表中.
我知道type这需要量身定做的,我已经能够使一个类的object使用下面的代码之类的,但我还没有找到一种方法来创建一个施法,让我这个添加到列表中..任何想法?
T与objType相同
public static List<T> LoadList(string fileName, Type objType)
{
List<T> objList = new List<T>();
object o = Activator.CreateInstance(objType);
objList.Add((**o.GetType()**)o);
return objList;
}
Run Code Online (Sandbox Code Playgroud)
如果这是一个更好的方式这样做我也开放的想法:)
django ×5
python ×4
c# ×3
reflection ×2
winforms ×2
activator ×1
arrays ×1
django-admin ×1
gunicorn ×1
pyramid ×1
python-2.7 ×1
typeof ×1