Setup Django Framework and first Django App
If you are using Windows, start a command shell with administrator privileges. Then run the command in your Django folder. If it is Linux, just run the command:
"python setup.py install"
This will install Django in your Python installation’s site-packages directory.
open the Python shell, then run:
>>> import django
>>> print(django.get_version())
1.5
create a Django project:
python C:\pythonXY\Scripts\django-admin.py startproject mysite
python /usr/local/bin/django-admin.py startproject mysite
go to Django project folder, start Django lightweight web server, listening on port 8080:
python manage.py runserver 8080
create the tables in the database, If you’re using SQLite, you don’t need to create anything beforehand - the database file will be created automatically when it is needed.
edit the settings.py file, if using SQLite, add:
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'c:/Python33/victor-test/mysite/my.db',
the syncdb command create tables in database, based on INSTALLED_APPS setting:
python manage.py syncdb
After you setup the project, you can create your app. A project can contain multiple apps.
To create your app, make sure you’re in the same directory as your mysite manage.py and type this command:
python manage.py startapp polls
Then, edit polls/models.py, to add in Poll and Choice classes:
from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)edit settings.py, and add polls to INSTALLED_APPS:
INSTALLED_APPS = (.... 'polls',)run this command to see the SQL commands:
python manage.py sql polls
run "python manage.py syncdb" to create the model tables.
To invoke the Python shell, use this command:
python manage.py shell
to check if you did everything correctly:
python manage.py cms check
Django admin sites are for your staff or clients to add, change and delete content. Django was written in a newsroom environment, with a very clear separation between “content publishers” and the “public” site. The admin is not intended to be used by site visitors. It’s for site managers.
Activate the admin site:
- Uncomment "django.contrib.admin" in the INSTALLED_APPS setting
- Run python manage.py syncdb. Since we have added a new application to INSTALLED_APPS, the database tables need to be updated
- Edit your mysite/urls.py file and uncomment the lines that reference to the admin
Setup Django CMS
To setup Django CMS on Linux:
sudo apt-get install python python-setuptools python-imaging
sudo easy_install pip
pip install django-cms
pip install south
Then, you start your project and run your webserver, using the Django method.
After that, you need to configure Django CMS in the Django project.
1) edit <project>/settings.py, add Django CMS and South to INSTALLED_APPS, make sure to uncomment 'django.contrib.admin'
2) edit <project>/settings.py, add Django CMS to the MIDDLEWARE_CLASSES
3) edit <project>/settings.py, add Django CMS to TEMPLATE_CONTEXT_PROCESSORS
4)edit <project>/settings.py, modify STATIC_ROOT
5)edit <project>/settings.py, modify TEMPLATE_DIRS
6)edit <project>/settings.py, add CMS_TEMPLATE
7)edit <project>/settings.py, modify DATABASES
8)edit <project>/urls.py, modify urlpatterns
9) add template files to <project>/templates/ folder
Django uses South for database migration. Models used in South are not synced to DB with syncdb. After you install South, you have to run:
python manage.py syncdb --all
python manage.py migrate --fake
python manage.py syncdb
python manage.py migrate
Django CMS use admin interface to add pages. Start the Django lightweight server:
python manage.py runserver 8000
Now, go to 127.0.0.1/admin/ and log in. You can use the admin interface to add pages and content.
No comments:
Post a Comment