• We just launched and are currently in beta. Join us as we build and grow the community.

Python - How To Connect To Database Server Using Django

bestifikni

Active Directory Auditor
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
139
Likes
105
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.

2017-07-28_09_39_55-c_windows_system32_cmd.exe__1.png


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.

2017-07-28_11_45_52-c_windows_system32_cmd.exe_.png

Creating 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'.

2017-07-28_13_16_54-2017-07-28_13_16_08-c_windows_system32_cmd.exe_.png_100_rgb_8_.png

Running 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:

2017-07-28_13_32_58-settings.png


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".

2017-07-28_14_03_21-c_windows_system32_cmd.exe_.png

Creating 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. # Create your models here.
  3. class

    Member(

    models.Model

    )

    :
  4. firstname =

    models.CharField

    (

    max_length=

    50

    )
  5. lastname =

    models.CharField

    (

    max_length=

    50

    )
  6. def

    __str__

    (

    self

    )

    :
  7. return

    self

    .firstname

    + " "

    + self

    .lastname

  8. class

    Subject(

    models.Model

    )

    :
  9. subject =

    models.ForeignKey

    (

    Member,

    on_delete=

    models.CASCADE

    )
  10. subject_title =

    models.CharField

    (

    max_length=

    100

    )
  11. def

    __str__

    (

    self

    )

    :
  12. 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.

2017-07-28_14_29_41-c_windows_system32_cmd.exe_-_manage.py_createsuperuser.png


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.

2017-07-28_14_36_16-c_windows_system32_cmd.exe_-_manage.py_runserver.png


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!!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top