GithubHelp home page GithubHelp logo

youngsoul / dj4-news Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 1013 KB

DjangoForBeginners Django 4.x

Dockerfile 0.21% Makefile 0.97% Python 5.17% Shell 0.15% HTML 1.99% Procfile 0.01% CSS 42.07% JavaScript 49.43%

dj4-news's Introduction

Simple Sample Django4 Docker Project Starter

The goal of this repo is to create an Django4.x/Postgres docker environment without you having to create a local python virtual environment. The virtual environment will be created in the image and the running container from the image.

This repo assumes you have Docker installed and can issue docker and docker-compose commands from a terminal window.

After cloning the repo, run the init_project.sh script. This will build the image, start the container and execute the django-admin startproject, in the running container.

To add new Python dependencies, update the requirements.in file, and rebuild the image.

pip-tools

This docker image uses pip-tools to manage dependencies. By placing the dependent module name with an optional version in the requirements.in file, the module and its dependencies will be installed into the Python virtual environment of the container.

Setup

  • clone this repo and in terminal window run:
git clone https://github.com/youngsoul/sample-django4-docker-project-starter.git <projectdirname>
  • review init_project.sh

Either run the script or execute each of the commands to get a base Django project.

Note that this will NOT run an initial migration because we want to create a customer User model BEFORE we run the initial migration.

chmod +x init_project.#!/bin/sh
./init_project.sh

PyCharm

You can now create a PyCharm project and set the Python Interpreter to the docker-compose.yml files selecting 'django-web' as the service

This will keep you from having to create a local venv just to install Django and call startproject.

Settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'djangodb',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'django-db', # set in docker-compose.yml
        'PORT': 5432
    }
}

When the script finishes, the docker containers for the Django WebServer and Postgres DB are running.

You can open a browser and go to:

http://localhost:8080

and you should see the familiar Django start page.

Custom User model

You almost always want to create a customer user model, BEFORE your initial migration.

  • Create a CustomUser model
  • Update django_project/settings.py
  • Customize UserCreationForm and UserChangeForm
  • Add the custom user model to admin.py
docker-compose exec django4-web python manage.py startapp accounts

or

make cmd="startapp accounts" manage
# accounts/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

class CustomUser(AbstractUser):
    pass
  • settings.py
# django_project/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Local
    "accounts"
]

# at the bottom of the file
AUTH_USER_MODEL = "accounts.CustomUser"
  • Make migrations
docker-compose exec django-web python manage.py makemigrations accounts

or

make migrations

or

make cmd="makemigrations accounts" manage
  • Run migrations
docker-compose exec django-web python manage.py migrate

or

make migrate

or

make cmd=migrate manage
  • Create Superuser
make superuser

dj4-news's People

Contributors

youngsoul avatar

Stargazers

li xinge avatar

Watchers

 avatar James Cloos avatar  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.