使用django-admin 上传-下载 文件

    xiaoxiao2021-11-30  55

    这段时间一直在弄Django,索性用Django弄一个资产管理的后台。那么从这里开始。

    怎么下载Django这些我就不多说了,现在直接开始创建项目了。

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 root@CD - FTP - VPN: / opt # django-admin startproject blog 我们进入到项目下 创建一个APP root@CD - FTP - VPN: / opt / blog # django-admin startapp jastme root@CD - FTP - VPN: / opt / blog # tree  . | - -  blog |   | - -  __init__.py |   | - -  __init__.pyc |   | - -  settings.py |   | - -  settings.pyc |   | - -  urls.py |   | - -  urls.pyc |   | - -  views.py |   | - -  views.pyc |   | - -  wsgi.py |   ` - -  wsgi.pyc | - -  db.sqlite3 | - -  jastme |   | - -  admin.py |   | - -  admin.pyc |   | - -  __init__.py |   | - -  __init__.pyc |   | - -  models.py |   | - -  models.pyc |   | - -  tests.py |   ` - -  views.py | - -  manage.py ` - -  start.sh

    vim blog/setting.py

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 """ Django settings for blog project.   For more information on this file, see https://docs.djangoproject.com/en/1.7/topics/settings/   For the full list of settings and their values, see https://docs.djangoproject.com/en/1.7/ref/settings/ """   # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import  os BASE_DIR  =  os.path.dirname(os.path.dirname(__file__))     # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/   # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY  =  'v=ui!#q&nimzj)k(p#8r1v)eiz7r$2rv6!8cca4anww^6bj*7w'   # SECURITY WARNING: don't run with debug turned on in production! DEBUG  =  True   TEMPLATE_DEBUG  =  True   ALLOWED_HOSTS  =  []     # Application definition   INSTALLED_APPS  =  (      'django.contrib.admin' ,      'django.contrib.auth' ,      'django.contrib.contenttypes' ,      'django.contrib.sessions' ,      'django.contrib.messages' ,      'django.contrib.staticfiles' ,      'jastme' , )   MIDDLEWARE_CLASSES  =  (      'django.contrib.sessions.middleware.SessionMiddleware' ,      'django.middleware.common.CommonMiddleware' ,      'django.middleware.csrf.CsrfViewMiddleware' ,      'django.contrib.auth.middleware.AuthenticationMiddleware' ,      'django.contrib.auth.middleware.SessionAuthenticationMiddleware' ,      'django.contrib.messages.middleware.MessageMiddleware' ,      'django.middleware.clickjacking.XFrameOptionsMiddleware' ,      'django.middleware.locale.LocaleMiddleware' , )   ROOT_URLCONF  =  'blog.urls'   WSGI_APPLICATION  =  'blog.wsgi.application'     # Database # https://docs.djangoproject.com/en/1.7/ref/settings/#databases   DATABASES  =  {      'default' : {          'ENGINE' :  'django.db.backends.mysql' , #        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),          'NAME' : 'blog' ,          'USER' : 'jastme' ,          'PASSWORD' : 'jastme' ,          'HOST' : 'localhost' ,          'PORT' : '3306' ,      } }     # Internationalization # https://docs.djangoproject.com/en/1.7/topics/i18n/   LANGUAGE_CODE  =  'en-us'   TIME_ZONE  =  'Asia/Shanghai'   USE_I18N  =  True   USE_L10N  =  True   USE_TZ  =  True     # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/   STATIC_URL  =  '/dist/' STATICFILES_DIRS  =  (                  os.path.join(BASE_DIR,  "dist" ),                  '/var/www/blog/dist/' , )   TEMPLATE_DIRS  = (                  '/var/www/blog/' , )   MEDIA_ROOT  =  '/var/www/blog/file' MEDIA_URL  =  '/file/'

    项目下的vim blog/urls.py

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 root@CD - FTP - VPN: / opt / blog # more blog/urls.py   from  django.conf.urls  import  patterns, include, url from  django.contrib  import  admin admin.autodiscover() from  django.conf  import  settings     urlpatterns  =  patterns('',      # Examples:      # url(r'^$', 'blog.views.home', name='home'),      # url(r'^blog/', include('blog.urls')),      url(r '^admin/' , include(admin.site.urls)),      url(r '^file/(?P<path>.*)$' ,  'django.views.static.serve' , { 'document_root' : settings.MEDIA_ROOT }), )

    APP下的vim jastme/models.py

    ? 1 2 3 4 5 6 7 8 9 10 11 root@CD - FTP - VPN: / opt / blog # more jastme/models.py   from  django.db  import  models   # Create your models here.   class  file (models.Model):      filename  =  models.FileField()        def  __unicode__( self ):          return  u 'self.filename'

     

    APP下的 vim jastme/admin.py

    ? 1 2 3 4 5 6 7 8 9 10 11 root@CD - FTP - VPN: / opt / blog # more jastme/admin.py   from  django.contrib  import  admin   # Register your models here. from  jastme.models  import  file   class  filelist(admin.ModelAdmin):      list_display  =  ( 'filename' ,)   admin.site.register( file ,filelist) ? 1 root@CD - FTP - VPN: / opt / blog # python manage.py syncdb

    点击增加

     

    点击保存就能上传

    再次点击文件下载

     

    这样文件就能下载了,非常方便快捷。

    转载请注明原文地址: https://ju.6miu.com/read-679158.html

    最新回复(0)