2012-04-02 3 views
0

어제 나는이 질문에 question을 물었다. Django-crispy 업로드 양식

나는 다음이 코드 조각을 발견 :

models.py

from django.db import models 
from app.extra import ContentTypeRestrictedFileField 

class upload(models.Model): 
    """ upload """ 
    name = models.CharField(max_length=100) 
    description = models.CharField(max_length=250) 
    file = ContentTypeRestrictedFileField(
     upload_to='/media/videos,' 
     content_types=['video/avi', 'video/mp4', 'video/3gp', 'video/wmp', 'video/flv', 'video/mov'], 
     max_upload_size=104857600 
    ) 
    created = models.DateTimeField('created', auto_now_add=True) 
    modified = models.DateTimeField('modified', auto_now=True) 

    def __unicode__(self): 
     return self.name 

form.py

from django.db.models import FileField 
from django.forms import forms 
from django.template.defaultfilters import filesizeformat 
from django.utils.translation import ugettext_lazy as _ 

class ContentTypeRestrictedFileField(FileField): 
    """ 
    Same as FileField, but you can specify: 
     * content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg'] 
     * max_upload_size - a number indicating the maximum file size allowed for upload. 
      2.5MB - 2621440 
      5MB - 5242880 
      10MB - 10485760 
      20MB - 20971520 
      50MB - 5242880 
      100MB 104857600 
      250MB - 214958080 
      500MB - 429916160 
    """ 
    def __init__(self, content_types=None,max_upload_size=104857600, **kwargs): 
     self.content_types = kwargs.pop('video/avi', 'video/mp4', 'video/3gp', 'video/wmp', 'video/flv', 'video/mov') 
     self.max_upload_size = max_upload_size 

     super(ContentTypeRestrictedFileField, self).__init__(**kwargs) 


    def clean(self, *args, **kwargs):   
     data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs) 

     file = data.file 
     try: 
      content_type = file.content_type 
      if content_type in self.content_types: 
       if file._size > self.max_upload_size: 
        raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size))) 
      else: 
       raise forms.ValidationError(_('Filetype not supported.')) 
     except AttributeError: 
      pass   

     return data 

     from south.modelsinspector import add_introspection_rules 
     add_introspection_rules([], ["^app\.extra\.ContentTypeRestrictedFileField"]) 

을이 라인은 settings.py

입니다 추가,451,515,
FILE_UPLOAD_MAX_MEMORY_SIZE = 157286400 # 157286400 bytes = 150 MB 

난 그냥 Django-crispy forms라는 응용 프로그램에 대해 알게되었습니다. 그들은 당신이 this snippetthis form

내 질문을 만들 줄 :

가 장고 - 바삭 다음과 같습니다 양식을 만들 내 조각을 결합 할 수 있습니까? django-tagging을 사용해야한다는 것을 알고 있습니다.

enter image description here

답변

0

은 무엇 CSS를 선택 장고 페이지의 양식을주고있다. 그런 다음 스타일 시트를 사용하여 모양을 변경하십시오. 선택기에서 직접 문서를보고 자세한 정보를 설정할 수도 있습니다.