Giter VIP home page Giter VIP logo

django-starter-with-models's Introduction

Django-starter-with-models

This basic starter for django I have gone through the steps of creating a project and app for future reference. This is a django application for saving employee information and rendering it to the webpage.

Pre-requisites:

  • Python
  • Django

For running the app:

python3 manage.py runserver

Steps for creating the same app:

1. Creating new site

➜ django-admin startproject employee_site

2. Creating new app

➜ django-admin startapp employees
  • ❗️ In the whole doc I will be referring employee_site as site and employees as app

  • ❗️ In the shell commands I have used python3 since I have a linux OS. If you are using windows use python instead of python3


3. Creating new view

  1. Add new view in views.py of app
  2. Create urls.py file in "app" directory
  3. Copy paste the following code in view.py
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def employees_page(request):
   return HttpResponse('Hello World')
  1. Add the url for view in urls.py
from django.urls import path
from .import  views
urlpatterns = [
	path('',views.employees_page, name="employees")
]
  1. Go to urls.py in "site" and add new path to the app using include method.
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('employees/', include("employees.urls")),
]
  1. Now we can go the url mentioned in the app we can see the response added in views.py
http://localhost:8000/employees/

4. Adding models

  1. Go to models.py in "app" and create new model:
from django.db import models

# Create your models here.
class employees_db(models.Model):
	emp_id = models.AutoField
	firstName = models.CharField(max_length=30)
	lastName = models.CharField(max_length=30)
	dept = models.CharField(max_length=30)
	salary = models.IntegerField()

	def __str__(self):
	 return self.firstName
  1. For adding the model inside to the site we have to add the appConfig inside the site's settings.py
  2. For copying the config first go to apps.py in "app" copy the "AppNameConfig" in our example it is EmployeesConfig. It wil be created automatically.
from django.apps import AppConfig


class EmployeesConfig(AppConfig):
    name = 'employees'
  1. Now go to settings.py in site and go to installed apps list and add "appName.apps.AppNameConfig" in our example it is EmployeesConfig.
INSTALLED_APPS = [
    'employees.apps.EmployeesConfig',
    'django.contrib.admin',
    ...
]
  1. Go to terminal and update the migrations and migrate. Make sure you are in the same directory as manage.py
➜ python3 manage.py makemigrations
➜ python3 manage.py migrate

Now our models are added to the database.


5. Accessing the admin panel

  1. First we have to create a new super user
➜ python3 manage.py createsuperuser
  1. Add the appropriate info and password
  2. Run server and
➜ python3 manage.py runserver
  1. Go to /admin page and add the admin name and password
http://localhost:8000/admin

6. Registering the models

  1. In order to use the models we have to register it
  2. For doing that go to admin.py in app
  3. Import our newly created model
  4. Register the model
  5. Code :
from django.contrib import admin
from .models import employees_db

# Register your models here.
admin.site.register(employees_db)
  1. Restart the server We can check the models filed in our admin panel now

7. Creating templates and adding it to views

  1. Create new folder named "templates" in app directory and add new html file
➜ mkdir templates
➜ touch index.html
  1. Now go to views.py in our app and add return statement which renders our html page in templates
return render(request, 'index.html',dbData)

8. Fetching the data from models

  1. First go to admin page:
http://localhost:8000/admin
  1. Now you can see that employees_db has been created we can click on it and add new employees
  2. Once it is done go to view.py in app
  3. Now first we will import our model which is employees_db and then store the db data to one variable and pass that data to the template as a third parameter.
  4. Code:
from django.shortcuts import render
from django.http import HttpResponse
from employees.models import employees_db

# Create your views here.
def employees_page(request):

	# fetching data from models
	dbData = {"data" : employees_db.objects.all()}

	return render(request, 'index.html',dbData)
  1. Now we can access data from Database through dbData variable.

9. Rendering data to templates

  1. Go to templates/ and open index.html
  2. We can use the data key passed in the dbData object/dictionary.
  3. We loop over it and render the values of attributes for eg. emp.firstName will give us firstName value.
<div class="container-style">
	{% for emp in data %}
	<div class="emp-card">
		<p class="id"><b>ID:</b> {{emp.id}}</p>
		<p class="name"><b>Name:</b> {{emp.firstName}} {{emp.lastName}}</p>
		<p class="dept"><b>Dept:</b> {{emp.dept}}</p>
		<p class="salary"><b>Salary:</b> $ {{emp.salary}}</p>
	</div>
	{% endfor %}
</div>
  1. You can add the optional styling which is included in my index.html page.

Check the employees page. For checking go to the following link:

http://localhost:8000/employees/

In the end our site will something like this:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.