Create Django application
Step 1. Django project set-up
django-admin startproject geeks
Step 2: Go to geeks by following the command cd geeks, and create a app.
python manage.py startapp gfg
Step 3: Now, we need to define it in “settings.py” in the project folder by adding the application name.
Add 'gfg,' under INSTALLED_APPS
Step 4: Migrate and deploy
python manage.py migrate
Step 5: Ensure app deployment in the deployment server http://127.0.0.1:8000/.
python manage.py runserver
Step 6: Create a templates folder in the parent folder “geeks“. Add “contact.html” in templates.
HTML
< div > < form method = "POST" > < h2 >w3wiki Tutorial</ h2 > {% csrf_token %} {{ form }} < button type = "submit" >Submit</ button > </ form > </ div > |
Step 7: Go to geeks/settings.py to add the following code
Python3
import os TEMPLATES_DIRS = os.path.join(BASE_DIR, 'templates' ) DIRS': [TEMPLATES_DIRS] |
Step 8: Create “gfg/forms.py” in gfg app to add the following code.
Python3
from django import forms from .models import w3wiki class geeks(forms.ModelForm): class Meta: model = w3wiki fields = [ "fullname" , "mobile_number" , ] labels = { 'fullname' : "Name" , "mobile_number" : "Mobile Number" , } |
Step 9: Open gfg/models.py and replace the existing code with the below code.
Python3
from django.db import models class w3wiki(models.Model): fullname = models.CharField(max_length = 200 ) mobile_number = models.IntegerField() |
Step 10: Replace contents in gfg/views.py with the following code.
Python3
from django.shortcuts import render from .models import w3wiki from .forms import geeks def gfgForm(request): if request.method = = "POST" : form = geeks(request.POST) if form.is_valid(): form.save() else : form = geeks() return render(request, 'contact.html' , { 'form' : form}) |
Step 11: Create gfg/urls.py in “gfg” folder and add the following code.
Python3
from . import views from django.urls import path urlpatterns = [ path(' ',views.gfgForm,name=' form') ] |
Step 12: Open geeks/urls.py to add the following.
Python3
from django.contrib import admin from django.urls import path, include urlpatterns = [ path( 'admin/' , admin.site.urls), path(' ', include(' gfg.urls')), ] |
Step 13: Open “admin.py” to add the following code which will register the model. This step will create a field in the admin dashboard of Django to view the data.
Python3
from django.contrib import admin from .models import w3wiki # Register your models here. admin.site.register(w3wiki) |
Step 14: Migrate all the data to an internal database to test our app.
python manage.py makemigrations python migrate
Step 15: Ensure app deployment in the deployment server http://127.0.0.1:8000/.
python manage.py runserver
Deploying Django App on Heroku with Postgres as Backend
Django is a high-level Python web framework used to create web applications without any hassle, whereas, PostgreSQL is a powerful, open-source object-relational database. Let us first create a Django application with PostgreSQL in the backend and deploy it in Heroku which is a container-based cloud Platform to deploy, manage, and scale the applications.
Contact Us