Giter VIP home page Giter VIP logo

py-django's Introduction

Django

Installation

This will install Django on your machine.

    pip install django

Initialize a django project

Create your repository folder then run:

    django-admin startproject myproject

Project structure

  1. manage.py: Runs commands for the project
  2. [projectname]/init.py: Tells python that the folder contains python code (also called dunder init)
  3. [projectname]/wsgi.py | asgi.py: Provides hooks for webservers such as Apache, Nginx, etc
  4. [projectname]/settings.py: Configures the Django project
  5. [projectname]/urls.py: Routes web requests based on URL

To run the project

Navigate to your project path where manage.py exists, then run:

    python manage.py runserver

This command will also create a SqLite database for your project.

What is a Django app

  • A component in a Django project
  • Each app fits a specific purpose e.g. Blog, Forum, Wiki etc
  • A folder with a set of python files

To create an app in your Django project, navigate to your project folder then run where your manage.py is:

    python manage.py startapp myapp

Add your app name in settings.py in your main project folder.

App's folder structure

File/folder Desc
apps.py settings related to the app (rare to use)
models.py database schema, queries, etc
admin.py administrative tasks
urls.py route options
views.py end user views
tests.py project tests
migrations/ db migrations related files

Django MVC architecture

URL Patterns (routes user's request to its relevant view) --> Views uses Models (logics, db communications, etc) --> Templates (Response contents)

Migration Commands

To run migration commands navigate to your main project folder where manage.py is, then run:

    python manage.py makemigrations # to create a new migration
    python manage.py showmigrations # to see your migrations
    python manage.py migrate # to apply your migrations
    python manage.py migrate <appname> <number> # to move to an specific migration

Admin configuration

Add your model to your admin.py file, then create your super user by running:

    from .models import Pet

    # Register your models here.
    @admin.register(Pet)
    class Admin(admin.ModelAdmin):
        pass
    python3 manage.py createsuperuser

Template Syntax

  • {{ variable }} e.g.

      <h3>{{ pet.name }}</h3>
  • {% tag %} e.g. for loop

      {% for pet in pets %}
          <li>{{ pet.name }}</li>
      {% endfor %}
  • {{ variable|filter }} e.g. datetime output formatting

      <h3>{{ pet.name|capfirst }}</h3>
  • some template tags doesn't have end tags e.g.

      {% url 'home' %} #output is /
      {% url 'pet_detail' pet.id %} #output /myapp/1/

py-django's People

Contributors

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