您的位置:首页 > 产品设计 > UI/UE

Build your first Django site - QA

2015-08-18 11:14 417 查看

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django python