Documents the setting up of Django dev environment
The following command wil create an environment directory in miniconda3/envs:
conda create --name
<Name of the environment> python=2.7
Go to the directory where you want project to be created.
Run the following command to create the project:
django-admin startproject
<project name>
You will noe see the following two when you run ls
--
<project name> and manage.py
Inside <project name> you wil see the following files:
-
init.py
-
settings.py
-
urls.py
-
wsgi.py
python manage.py migrate
Now, you will now see a database there:
<project name> db.sqlite3 manage.py
Finally,
python manage.py runserver
. Test it by going to http://127.0.0.1:8000/
python manage.py startapp browseCatalog
python manage.py check
python manage.py makemigrations browseCatalog
python manage.py sqlmigrate browseCatalog 0001
The sqlmigrate command doesn’t actually create the tables or otherwise touch your database – it just prints output to the screen so you can see what SQL Django would execute if you asked it.
python manage.py migrate
p1 = Publisher(...)
# At this point, p1 is not saved to the database yet!
p1.save()
# Now it is.
If you want to create an object and save it to the database in a single step, use the objects.create() method.
The DoesNotExist exception is an attribute of the model’s class – Publisher.DoesNotExist. In your applications, you’ll want to trap these exceptions, like this:
try:
p = Publisher.objects.get(name='Apress')
except Publisher.DoesNotExist:
print ("Apress isn't in the database yet.")
else:
print ("Apress is in the database.")
python manage.py createsuperuser
from django.contrib import admin
from .models import Publisher, Author, Book
admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)