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 first1. 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 installpip install django-registration-reduxin your virtualenv.
Add to INSTALLED_APPS
Like config other apps in django, add app ‘registration’ to your settings.pyINSTALLED_APPS = ( ... 'registration', )
Add account email activation settings.
Still in settings.pyACCOUNT_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.pyTEMPLATES = [ { '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.pyurlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^accounts/', include('registration.backends.default.urls')), ]
Start server and test
Then runpython 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.
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法