Build your first Django site - QA

How to change admin password ?

Django provided a built-in admin module, we can change our password from command line:

python manage.py changepassword <username>

if you even forgot your username, then create another superuser:

python manage.py createsuperuser

How to set up virtualenv for project development?

There are two ways, basiclly. install virtualenv first

1. Basic
pip install virtualenv

Then, you need cd into your project folder, and then set up virtualenv using:

virtualenv <env>

This command will generate all virtualenv supported files under folder /,
use actiave and deactivate to start or stop virtualenv.

2. Use virtualenvwrapper
pip install virtualenvwrapper

virtualenvwrapper is a global virtualenv manager. you can create multiple virtualenvs, and switch among them. Before use this tool, you should change your ~/.profile:

source "/usr/local/bin/virtualenvwrapper.sh"
export WORKON_HOME=~/Envs

Envs contains all the virtualenv settings.

Add a virtualenv:

mkvirtualenv <name>

then work on a virtualenv:

workon <name>

Deactivate by

deactivate

virtualenvmapper also provide more helpful commands for env, like lsvirtualenvs, rmvirtualenv.

See http://virtualenvwrapper.readthedocs.org/en/latest/ for more.

How to use Django-registration-redux?

The previous django-registration package is not work for django 1.8, we need to use Django-registration-redux.

Install django-registration-redux

Of course install pip install django-registration-redux in your virtualenv.

Add to INSTALLED_APPS

Like config other apps in django, add app ‘registration’ to your settings.py

INSTALLED_APPS = (
   ...
   'registration',
)
Add account email activation settings.

Still in settings.py

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; 
REGISTRATION_AUTO_LOGIN = True # Automatically log the user in.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxxxxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
EMAIL_PORT = 587

EMAIL_BACKEND has more options, actually this setting is here https://docs.djangoproject.com/en/1.8/topics/email/, not related to the django-registration-redux package.
Well, it is not good to add our email and password in settings.py, I will definitely improve it.

Ok, go on, we need our templates, for django1.8, specify templates as follow:

Add TEMPLATES

Still in settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ os.path.join(os.path.dirname('__file__'), "templates") ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Along with it, create a templates folder under your project folder, and then download templates from here https://github.com/macdhuibh/django-registration-templates .

Add urls

Don’t forget to set urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('registration.backends.default.urls')),
]
Start server and test

Then run

python manage.py migrate 
python manage.py syncdb
python manage.py runserver 8000

Your server is up, what you can do now is:

http://localhost:8000/accounts/login

you will see registration, activation, reset, login and so on.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值