上传下载图片,django-filter

上传下载图片

上传图片

在models中:
avatar = models.ImageField(verbose_name="头像", upload_to='users/%Y/%m/%d/', 
    max_length=128, null = True, blank = True)

在settings.py中
MEDIA_ROOT = os.path.join(BASE_DIR,"media") 

MEDIA_URL = '/media/'

在html中
{% if form.errors.avatar %} {% endif %}

下载图片

在settings.py:
TEMPLATES = [{
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.media',
         ],
      },
  },
]


在项目根目录urls.py:
from qfgp01site.settings import MEDIA_ROOT
from django.views.static import serve

urlpatterns = [
    re_path(r'^media/(?P.*)$', serve, {"document_root": MEDIA_ROOT}),
]

在html中
头像

django-filter

进行信息过滤

流程:
html的ajax请求-->views-->cmdb_django_filter(filter的过滤选项)-->views.reutrn-->html

pip3 install django-filter
pip3 install django-widget-tweaks


在settings中注册应用:
'django_filters',
'widget_tweaks',

在views:
def server_list(request):
    f = ServerFilter(request.GET, queryset=Server.objects.all())
    return render(request, 'cmdb/server-list.html', {"filter": f}) 

在cmdb_django_filter:
class ServerFilter(django_filters.FilterSet):
    class Meta:
        model = Server
        fields = {
            'host_name': ['exact', ],
            'physical_count': ['lt', 'gt'],
            'kernel': ['exact']
        }

html中:
{% load widget_tweaks %}

{{ filter.form.host_name.label_tag }} {{ filter.form.host_name|add_class:"form-control" }} {{ filter.form.physical_count__lt.label_tag }} {{ filter.form.physical_count__lt|add_class:"form-control" }} {{ filter.form.physical_count__gt.label_tag }} {{ filter.form.physical_count__gt|add_class:"form-control" }}

你可能感兴趣的:(上传下载图片,django-filter)