Creating Django Project
Goto Terminal
Activate virtual environment *
$ django-admin.py startproject <project_name>
eg:- django-admin.py startproject myproject
This will create folder stricture like the bellow
myproject/ |----: manage.py |----: myproject/ |---: __init__.py |---: settings.py |---: urls.py |---: wsgi.py
How to run Django project
$ python manage.py runserver
How to Create an App
$ python manage.py startapp <app_name>
eg:-
$ python manage.py startapp myapp
This will create folder stricture like the bellow
myproject/ |----: manage.py |----: myproject/ |---: __init__.py |---: settings.py |---: urls.py |---: wsgi.py |----: myapp/ |---: __init__.py |---: admin.py |---: models.py |---: test.py |---: views.py
add project in settings.py (myproject/myproject/settings.py and add app name in INSTALLED_APP) # Application definition INSTALLED_APPS = ( #default settings 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', #settings we need to add 'myapp' )
How to implement model
add model details in models.py (eg:- /myproject/myapp/models.py)
class Person(models.Model):
name = models.CharField(max_length=200)
adderess = models.TextField()
reg_date = models.DateTimeField(‘date registerd’)
def __str__(self):
return self.name
in the above example Person will be table name and all the variables inside the class will be the table field name (name, adderss,reg_date), by default django will create sqlite datablse (default database settings in settings.py)
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
}
ref:-https://docs.djangoproject.com/en/dev/topics/db/models/
$ python manage.py sql polls
$ python manage.py syncdb
$ python manage.py runserver