Python 使用 wsgi.py 和 apache2 的 Django 每次重新启动时都会乱序加载过滤器字段
Python Django using wsgi.py with apache2 loads filter fields out of order every restart
我有一个 Django 应用程序,当 运行 python manage.py 在本地运行服务器命令时,它工作正常。当我尝试迁移它并修改我的 apache 配置文件以使用 wsgi.py 时,除了 Django 过滤器字段在 Django table 之上的一页之外,一切正常。每次我重新启动 apache 服务器时,过滤器字段都会以新的顺序重新洗牌。本地服务器总是以正确的顺序显示过滤器字段。有谁知道为什么会这样?
本地服务器使用 python runserver 命令
使用 wsgi 的 apache 服务器
apache2.conf
WSGIDaemonProcess /SeedInv python-home=/path/to/Software/anaconda3/envs/Django python-path=/path/to/WebServer/SeedInv
WSGIProcessGroup /SeedInv
WSGIScriptAlias /SeedInv /path/to/WebServer/SeedInv/SeedInv/wsgi.py process-group=/SeedInv
<Directory /path/to/WebServer/SeedInv/SeedInv>
<Files /path/to/WebServer/SeedInv/Seedinv/wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static "/path/to/WebServer/SeedInv/static"
<Directory "/path/to/WebServer/SeedInv/static">
Require all granted
</Directory>
Alias /media "/path/to/WebServer/SeedInv/media"
<Directory "/path/to/WebServer/SeedInv/media">
Require all granted
</Directory>
wsgi.py
import os
import sys
sys.path.append('/path/to/anaconda3/envs/Django/lib/python3.6/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SeedInv.settings")
application = get_wsgi_application()
filters.py
class GenoFilter(django_filters.FilterSet):
class Meta:
model = Genotypes
fields = {'parent_f_row': ['icontains'],
'parent_m_row': ['icontains'],
'parent_f_geno': ['icontains'],
'parent_m_geno': ['icontains'],
'genotype': ['icontains'],
'seed_count': ['lt', 'gt'],
}
tables.py
class GenotypesTable(tables.Table):
export_formats = ['tsv']
class Meta:
model = Genotypes
template_name = 'django_tables2/bootstrap.html'
views.py
def InventoryTable(request):
queryset = Genotypes.objects.all()
f = GenoFilter(request.GET, queryset=queryset)
table = GenotypesTable(f.qs)
RequestConfig(request, paginate={'per_page': 25}).configure(table)
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, table)
return exporter.response('table.{}'.format(export_format))
return render(request, 'Inventory/index.html',
{
'table': table,
'filter': f,
})
models.py
# Genotype database model
class Genotypes(models.Model):
"""list of current genotypes"""
parent_f_row = models.CharField(max_length=200,
validators=[],)
parent_m_row = models.CharField(max_length=200,
validators=[],)
parent_f_geno = models.CharField(max_length=200,
validators=[],)
parent_m_geno = models.CharField(max_length=200,
validators=[],)
genotype = models.CharField(max_length=200,
validators=[],)
seed_count = models.IntegerField(validators=[], default=0)
actual_count = models.BooleanField(default=False)
experiment = models.CharField(max_length=200,
validators=[],
blank=True,)
comments = models.CharField(max_length=300,
validators=[],
blank=True,
)
def __str__(self):
return self.genotype
class Meta:
verbose_name_plural = "Genotypes"
感谢您的帮助!
,你不应该依赖被订购的字典。如果键的顺序很重要,您可以使用 OrderedDict
。
from collections import OrderedDict
class GenoFilter(django_filters.FilterSet):
class Meta:
model = Genotypes
fields = OrderedDict([
('parent_f_row', ['icontains']),
('parent_m_row', ['icontains']),
('parent_f_geno', ['icontains']),
('parent_m_geno', ['icontains']),
('genotype', ['icontains']),
('seed_count', ['lt', 'gt']),
])
我有一个 Django 应用程序,当 运行 python manage.py 在本地运行服务器命令时,它工作正常。当我尝试迁移它并修改我的 apache 配置文件以使用 wsgi.py 时,除了 Django 过滤器字段在 Django table 之上的一页之外,一切正常。每次我重新启动 apache 服务器时,过滤器字段都会以新的顺序重新洗牌。本地服务器总是以正确的顺序显示过滤器字段。有谁知道为什么会这样?
本地服务器使用 python runserver 命令
使用 wsgi 的 apache 服务器
apache2.conf
WSGIDaemonProcess /SeedInv python-home=/path/to/Software/anaconda3/envs/Django python-path=/path/to/WebServer/SeedInv
WSGIProcessGroup /SeedInv
WSGIScriptAlias /SeedInv /path/to/WebServer/SeedInv/SeedInv/wsgi.py process-group=/SeedInv
<Directory /path/to/WebServer/SeedInv/SeedInv>
<Files /path/to/WebServer/SeedInv/Seedinv/wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static "/path/to/WebServer/SeedInv/static"
<Directory "/path/to/WebServer/SeedInv/static">
Require all granted
</Directory>
Alias /media "/path/to/WebServer/SeedInv/media"
<Directory "/path/to/WebServer/SeedInv/media">
Require all granted
</Directory>
wsgi.py
import os
import sys
sys.path.append('/path/to/anaconda3/envs/Django/lib/python3.6/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SeedInv.settings")
application = get_wsgi_application()
filters.py
class GenoFilter(django_filters.FilterSet):
class Meta:
model = Genotypes
fields = {'parent_f_row': ['icontains'],
'parent_m_row': ['icontains'],
'parent_f_geno': ['icontains'],
'parent_m_geno': ['icontains'],
'genotype': ['icontains'],
'seed_count': ['lt', 'gt'],
}
tables.py
class GenotypesTable(tables.Table):
export_formats = ['tsv']
class Meta:
model = Genotypes
template_name = 'django_tables2/bootstrap.html'
views.py
def InventoryTable(request):
queryset = Genotypes.objects.all()
f = GenoFilter(request.GET, queryset=queryset)
table = GenotypesTable(f.qs)
RequestConfig(request, paginate={'per_page': 25}).configure(table)
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, table)
return exporter.response('table.{}'.format(export_format))
return render(request, 'Inventory/index.html',
{
'table': table,
'filter': f,
})
models.py
# Genotype database model
class Genotypes(models.Model):
"""list of current genotypes"""
parent_f_row = models.CharField(max_length=200,
validators=[],)
parent_m_row = models.CharField(max_length=200,
validators=[],)
parent_f_geno = models.CharField(max_length=200,
validators=[],)
parent_m_geno = models.CharField(max_length=200,
validators=[],)
genotype = models.CharField(max_length=200,
validators=[],)
seed_count = models.IntegerField(validators=[], default=0)
actual_count = models.BooleanField(default=False)
experiment = models.CharField(max_length=200,
validators=[],
blank=True,)
comments = models.CharField(max_length=300,
validators=[],
blank=True,
)
def __str__(self):
return self.genotype
class Meta:
verbose_name_plural = "Genotypes"
感谢您的帮助!
OrderedDict
。
from collections import OrderedDict
class GenoFilter(django_filters.FilterSet):
class Meta:
model = Genotypes
fields = OrderedDict([
('parent_f_row', ['icontains']),
('parent_m_row', ['icontains']),
('parent_f_geno', ['icontains']),
('parent_m_geno', ['icontains']),
('genotype', ['icontains']),
('seed_count', ['lt', 'gt']),
])