from django.contrib.auth import authenticate def some_view(request): user = authenticate(username='john', password='secret') if user is not None: # the password verified for the user if user.is_active: print("User is valid, active and authenticated") else: print("The password is valid, but the account has been disabled!") else: # the authentication system was unable to verify the username and password print("The username and password were incorrect.")
from django.contrib.auth.models import User def some_view(request): u = User.objects.get(username='john') u.set_password('new password') u.save()
ref :-https://docs.djangoproject.com/en/1.5/topics/auth/default/#changing-passwords
from django.contrib.auth.models import User def some_view(request): user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword') # if you want to change other fields. user.last_name = 'Lennon' user.save()
ref:-https://docs.djangoproject.com/en/1.5/topics/auth/default/#creating-users
Open admin.py in app folder and past this code
from django.contrib import admin
from myapp.models import Person
admin.site.register(Person)
—
Person :- this is a class defined in models (myapp/models.py)
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
Installing an official release with pip
Goto terminal
Activate virtual environment *
pip install Django
Installation doc:- https://docs.djangoproject.com/en/dev/topics/install/#installing-official-release
Doc :- https://docs.djangoproject.com/en/dev/contents/