结合使用wsgi.py和apache2的Python Django每次重新启动时都会无序加载过滤器字段

时间:2018-12-13 14:59:24

标签: python django apache wsgi

我有一个django应用,当在本地运行python manage.py runtimeever命令时,该应用运行良好。当我尝试迁移它并修改我的apache配置文件以使用wsgi.py时,除django过滤器字段在django表上方随机排列的一页外,其他所有东西都起作用。每次重新启动apache服务器时,过滤器字段都会以新的顺序重新排列。本地服务器始终以正确的顺序显示过滤器字段。有人知道为什么会这样吗?

使用python runserver命令的本地服务器 enter image description here

使用wsgi的Apache服务器 enter image description here

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"

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

Until Python 3.7,您不应依赖于所订购的字典。如果键的顺序很重要,则可以使用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']),
        ])