• Linux下开发python django程序(设置admin后台管理模块)


    1.新建项目和项目下APP

    django-admin startproject csvt03
    django-admin startapp app1


     

    2.修改settings.py文件

    设置默认安装APP

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'app1',
        # Uncomment the next line to enable the admin:
        'django.contrib.admin',
        # Uncomment the next line to enable admin documentation:
        # 'django.contrib.admindocs',
    )

    设置数据库为sqlite3

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'csvt03.db',                      # Or path to database file if using sqlite3.
            'USER': '',                      # Not used with sqlite3.
            'PASSWORD': '',                  # Not used with sqlite3.
            'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        }
    }

    3.在models.py文件中新建数据库表映射

    sex_choices=(
    ('f','famale'),('m','male')
    )
    class User(models.Model):
            name = models.CharField(max_length=30)
            sex=models.CharField(max_length=1,choices=sex_choices)
            def __unicode__(self):
                    return self.name

    4.编辑urls.py文件

    from django.conf.urls.defaults import patterns, include, url
    
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'csvt03.views.home', name='home'),
        # url(r'^csvt03/', include('csvt03.foo.urls')),
    
        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        url(r'^admin/', include(admin.site.urls)),
    )


    5.生成数据文件

    python manage.py syncdb
    Creating tables ...
    Creating table auth_permission
    Creating table auth_group_permissions
    Creating table auth_group
    Creating table auth_user_user_permissions
    Creating table auth_user_groups
    Creating table auth_user
    Creating table auth_message
    Creating table django_content_type
    Creating table django_session
    Creating table django_site
    Creating table app1_user
    
    You just installed Django's auth system, which means you don't have any superusers defined.
    Would you like to create one now? (yes/no): no
    Installing custom SQL ...
    Installing indexes ...
    No fixtures found.

    6. 查看生成后的sqlite3数据库文件

    sqlite3 csvt03.db

    7.在app1中添加admin.py文件,并在文件中添加管理员模块数据库映射

    from django.contrib import admin
    from app1.models import User
    
    admin.site.register(User)
    

    8运行开发服务器:

    python manage.py runserver

    访问127.0.0.1:8000/admin

  • 相关阅读:
    C++类中使用new及delete小例子(续)
    C++类中使用new及delete小例子
    C++类中static修饰的函数的使用
    C++类使用static小例子(新手学习C++)
    C++结构体中使用函数与类中使用函数小结
    记一次简单的性能优化
    [转载]Java的内存回收机制
    写给自己的项目总结
    [转载]正则表达式30分钟入门教程
    使用JRockit进行性能优化一:环境搭建
  • 原文地址:https://www.cnblogs.com/whzym111/p/5891470.html
Copyright © 2020-2023  润新知