Build your first Django site - QA

来源:互联网 发布:手机qq苹果在线软件 编辑:程序博客网 时间:2024/05/28 15:04

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 = TrueEMAIL_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 syncdbpython 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.

0 0