This is the continuation from previous post, where we learned how to install Django and run server. Now lets create first app using Django.
Before creating our first app lets try to login to Django server has ADMIN. For that we need to create one super user account.
python manage.py createsuperuser
Above command will ask for Username, Email, Password. I recommend to keep default user name and Email ID, however Password you need to specify.
After creating Super user successfully, you need to migrate the changes you have done.
python manage.py showmigrations
Above command will shows the list of change or need to migrate.
python manage.py migrate
Assuming our server is still running in one side, we will open below mentioned link:
Use your user name and password you previously created. If you are able to login successfully, then you seen site administration layout.
If you click on User then you will see your super user details.
Lets create our first app – Django
Will create one Directory called src. And Run the below command:
django-admin startproject trydjango . (This is the same command used in the previous post, you can use existing or you can Re – create one).
At first we need to understand files created under trydjango directory. In brief we will move directly towards only one file called setting.py . Here in setting.py is used for application definitions.
In setting.py you will see one line called INSTALLED_APPS where it is used to define your models/project definitions here.
Lets create another module called “Products”. for that you need to run below command.
python manage.py startapp products
Above command will create new project module with respect to trydjango project. Once command is ran successfully some python files are created like models.py,admin.py etc.
But we will use mainly models.py and admin.py for our project. Lets start coding.
Open models.py and add below mentioned code.
Above are the built-in function of Django where you can find more details here.
And also you need to register you model called “Product” should be registered in the Admin.py file.
After that adding your app or hooking up you product module in the setting.py file so that it can directed toward Django server.
Next task is to run one command whenever changes are done to any files in the directory, otherwise changes will not be migrated to DB or you root file.
python manage.py migrate
Once migration is done successfully, assuming our server is running in one side, we will move to “http://127.0.0.1:8000/admin/”
Click on add button. Then you will see three text box where you created in the models.py file.
Add the values and click on save. Then you will see the Content you enter in the
Text box has been saved successfully.
So finally we are done with creating First using Django.