Giter Site home page Giter Site logo

python-django-practice's Introduction

python-django-practice

Python Django Projects

Django Setup Steps

  1. Create Virtual Environment (virtualenv .venv - for mac, for windows - python -m venv .venv)
  2. Activate Virtual Environment (source .venv/bin/activate | .venv/Scripts/activate)
  3. Install Django (pip install django). (You can also install all the requirements file)
  4. Start django project in the current directory (django-admin startproject project_name .)
  5. Create new database in mysql (mypythondjangopractice)
  6. Update settings file - DATABASES DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mypythondjangopractice', # Name of your MySQL database 'USER': 'username', # MySQL username 'PASSWORD': 'userpassword', # MySQL password 'HOST': 'hostname', # MySQL server host (change to your MySQL server address) - localhost for local 'PORT': '3306', # MySQL server port (default is usually 3306) 'ATOMIC_REQUESTS': True, } }
  7. Run command - python manage.py migrate
  8. Run server - python manage.py runserver (http://127.0.0.1:8000/)

Create first django app (common)

  1. Create apps/common/init.py
  2. create apps/common/apps.py Copy this code in apps.py `from django.apps import AppConfig

class CommonConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "apps.common" 3. Create apps/common/urls.py Copy this code in urls.py - We will create a healthcheck apifrom django.urls import path

from apps.common import views

urlpatterns = [ path("", views.HealthCheck.as_view(), name="health"), ] 4. Create a view for this in apps/common/views.py We will be using djangorestframework for APIs (Install requirements.txt and add inINSTALLED_APPS-'rest_framework') Copy this code in views.py author = "Deepak Verma"

from rest_framework import status from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.views import APIView

all = ("HealthCheck",)

class HealthCheck(APIView): permission_classes = [AllowAny]

def get(self, request, format=None):
    try:
        return Response(
            {
                "is_success": True,
                "message": "Servers are running...",
                "data": None,
            },
            status=status.HTTP_200_OK,
        )
    except Exception as ex:
        return Response(
            {"is_success": False, "message": None, "data": None},
            status=status.HTTP_200_OK,
        )

5. Modify project_name/urls.py to add the above route tourlpatterns. path('', include('apps.common.urls')),6. Addapps.commontoINSTALLED_APPS` 7. Run Server

python-django-practice's People

Contributors

vrmadeepak avatar

Watchers

 avatar

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.