Python - How To Connect To Database Server Using Django

In this tutorial we will try to Connect To Database Using Django. Django is a free and open source web application framework, written in Python. A web framework is a set of components that helps you to develop websites faster and easier. Django makes developers life convenient and productive framework to all. So let's now do the coding... Getting started First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter. tut1 Wait for the django to be downloaded and installed at the same time. Then After that type "python -m django version" to check if django is installed and what version of django is. tut2Creating the App After setting django we will now create the web app for the database server. First cd into a directory where you’d like to store your code, then type "django-admin startproject Tutorial". A new folder will be created on the directory named 'Tutorial'. tut3Running The Server After creating a project, cd to the newly created directory, then type "manage.py runserver" to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework. Here is the image of python web server: tut4 Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors. Creating The Website This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp mysite". A new directory will be create inside the app named "mysite". tut5Creating The Database Now that the web app is working we will now then create the database. First go to mysite directory then open "models.py" to python IDLE's or any text editor. Then copy/paste the code below inside the "models.py" file.
  1. from django.db import models
  2.  
  3. # Create your models here.
  4. class Member(models.Model):
  5. firstname = models.CharField(max_length=50)
  6. lastname = models.CharField(max_length=50)
  7. def __str__(self):
  8. return self.firstname + " " + self.lastname
  9.  
  10. class Subject(models.Model):
  11. subject = models.ForeignKey(Member, on_delete=models.CASCADE)
  12. subject_title = models.CharField(max_length=100)
  13. def __str__(self):
  14. return self.subject_title
Then go to Tutorial directory, and open "settings.py". To do this just add this line code to the INSTALLED_APPS variable. 'mysite.apps.MysiteConfig'
  1. INSTALLED_APPS = [
  2. 'mysite.apps.MysiteConfig',
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
Then after that open the command prompt, and go to the Tutorial directory type 'manage.py makemigrations mysite' and hit enter. It will connect the newly database to the server, after doing that type 'manage.py migrate', and then hit enter to activate the database models. Creating The Admin Account After setting up the Database we will the create the admin to authorize that managing of the database. Type ''manage.py createsuperuser" and hit enter, then fill up the required field. tut6 Now that all is setup, start again the server via command prompt 'manage.py runserver'. Then type "http://127.0.0.1:8000/admin/" in the url address. After that login the account that you enter a while ago. tut7 Then that's it you finally connected to the Python Database Server Using Django Web Framework . I hope that this simple tutorial help you understand how django works. For more updates and tutorials just kindly visit this site, Enjoy Coding!!!

Tags

Add new comment