diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43c67e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +venv +*.pyc +staticfiles +.env +db.sqlite3 +getting-started/* diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..b6f8930 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn gettingstarted.wsgi diff --git a/Procfile.windows b/Procfile.windows new file mode 100644 index 0000000..69789e5 --- /dev/null +++ b/Procfile.windows @@ -0,0 +1 @@ +web: python manage.py runserver 0.0.0.0:5000 diff --git a/README.md b/README.md new file mode 100644 index 0000000..db5bd7d --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Django + +ما با استفاده از Django یک اپلیکیشن آزمایشی ساخته‌ایم که می‌توانید آن را روی کانتینر ابری قرار دهید. + +برای امتحان کردن این امکان، از ریپازیتوری Django در گیت‌هاب یک انشعاب بگیرید (Fork) تا یک نسخه‌ی کپی از آن را روی ابرک خود داشته باشید. + +در مرحله‌ی بعد، با قرار دادن این ریپازیتوری به عنوان منبع، یک اپ بسازید. کانتینر ابری کد را بررسی خواهد کرد و به طور خودکار، اجزایی را که لازم است بسازد تشخیص خواهد داد. سپس، Buildpack مناسب را به کار خواهد برد تا یک کانتینر بسازد و دیپلوی کند. بعد از این قسمت، می‌توانید تغییرات را روی انشعاب خود Push کنید و خواهید دید که کانتینر ابری به طور خودکار به‌روزرسانی‌ها را روی اپ شما مجددا اعمال خواهد کرد. + +## روش استفاده + +نسخه‌ی پیشفرض پایتون در حال حاضر ۳.۹.۴ است. اگر می‌خواهید از نسخه‌ی پایتون متفاوتی استفاده کنید، در ابتدایی‌ترین مرحله‌ی ریپازیتوری خود یک فایل `runtime.txt` بسازید و در یک خط کد، نسخه‌ی پایتون مورد نظر خود را با فرمت `python-` اضافه کنید. به عنوان مثال در صورت استفاده از پایتون ۳.۸.۸، `runtime.txt` + شما به شکل زیر خواهد بود: + +``` +python-3.8.8 +``` + +## محدودیت‌ها + +در صورت دیپلوی کردن دیپازیتوری Django در حالتی که از Gunicorn استفاده می‌شود، کانتینر ابری تنها بخشی از دستور اجرا را تشخیص خواهد داد. در این صورت لازم است شما به روش زیر نقطه‌ی ورود (Entry Point) اپ خود را اضافه کنید: + +``` +gunicorn --worker-tmp-dir /dev/shm mysite.wsgi. +``` diff --git a/gettingstarted/__init__.py b/gettingstarted/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gettingstarted/settings.py b/gettingstarted/settings.py new file mode 100644 index 0000000..9ea552a --- /dev/null +++ b/gettingstarted/settings.py @@ -0,0 +1,119 @@ +""" +Django settings for gettingstarted project. + +Generated by 'django-admin startproject' using Django 2.0. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.0/ref/settings/ +""" + +import os +import django_heroku + + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "CHANGE_ME!!!! (P.S. the SECRET_KEY environment variable will be used, if set, instead)." + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "hello", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "gettingstarted.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ] + }, + } +] + +WSGI_APPLICATION = "gettingstarted.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/2.0/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE" : "django.db.backends.sqlite3", + "NAME": os.path.join(BASE_DIR, "db.sqlite3") + } +} + +# Password validation +# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" + }, + {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"}, + {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, + {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.0/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.0/howto/static-files/ + +STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') +STATIC_URL = "/static/" + +django_heroku.settings(locals()) diff --git a/gettingstarted/static/humans.txt b/gettingstarted/static/humans.txt new file mode 100644 index 0000000..e69de29 diff --git a/gettingstarted/urls.py b/gettingstarted/urls.py new file mode 100644 index 0000000..13b7595 --- /dev/null +++ b/gettingstarted/urls.py @@ -0,0 +1,21 @@ +from django.urls import path, include + +from django.contrib import admin + +admin.autodiscover() + +import hello.views + +# To add a new path, first import the app: +# import blog +# +# Then add the new path: +# path('blog/', blog.urls, name="blog") +# +# Learn more here: https://docs.djangoproject.com/en/2.1/topics/http/urls/ + +urlpatterns = [ + path("", hello.views.index, name="index"), + path("db/", hello.views.db, name="db"), + path("admin/", admin.site.urls), +] diff --git a/gettingstarted/wsgi.py b/gettingstarted/wsgi.py new file mode 100644 index 0000000..3c528d7 --- /dev/null +++ b/gettingstarted/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for gettingstarted project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/ +""" + +import os + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings") + +from django.core.wsgi import get_wsgi_application + +application = get_wsgi_application() diff --git a/hello/__init__.py b/hello/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hello/admin.py b/hello/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/hello/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/hello/migrations/0001_initial.py b/hello/migrations/0001_initial.py new file mode 100644 index 0000000..99c4a82 --- /dev/null +++ b/hello/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-01-27 21:54 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Greeting', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('when', models.DateTimeField(auto_now_add=True, verbose_name=b'date created')), + ], + ), + ] diff --git a/hello/migrations/__init__.py b/hello/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hello/models.py b/hello/models.py new file mode 100644 index 0000000..8229092 --- /dev/null +++ b/hello/models.py @@ -0,0 +1,5 @@ +from django.db import models + +# Create your models here. +class Greeting(models.Model): + when = models.DateTimeField("date created", auto_now_add=True) diff --git a/hello/static/lang-logo.png b/hello/static/lang-logo.png new file mode 100644 index 0000000..f04aff1 Binary files /dev/null and b/hello/static/lang-logo.png differ diff --git a/hello/templates/base.html b/hello/templates/base.html new file mode 100644 index 0000000..ee94f6c --- /dev/null +++ b/hello/templates/base.html @@ -0,0 +1,73 @@ + + + + + + + Hello World + + + + + +{% block content %}{% endblock %} + + + diff --git a/hello/templates/db.html b/hello/templates/db.html new file mode 100644 index 0000000..2d3a390 --- /dev/null +++ b/hello/templates/db.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} +{% load static %} + +{% block content %} +
+ + +

Page View Report

+ + + + +
+ +{% endblock %} diff --git a/hello/templates/index.html b/hello/templates/index.html new file mode 100644 index 0000000..75f370b --- /dev/null +++ b/hello/templates/index.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% load static %} + +{% block content %} + + + +{% endblock %} diff --git a/hello/tests.py b/hello/tests.py new file mode 100644 index 0000000..9b06366 --- /dev/null +++ b/hello/tests.py @@ -0,0 +1,19 @@ +from django.contrib.auth.models import AnonymousUser, User +from django.test import TestCase, RequestFactory + +from .views import index + + +class SimpleTest(TestCase): + def setUp(self): + # Every test needs access to the request factory. + self.factory = RequestFactory() + + def test_details(self): + # Create an instance of a GET request. + request = self.factory.get("/") + request.user = AnonymousUser() + + # Test my_view() as if it were deployed at /customer/details + response = index(request) + self.assertEqual(response.status_code, 200) diff --git a/hello/views.py b/hello/views.py new file mode 100644 index 0000000..c248072 --- /dev/null +++ b/hello/views.py @@ -0,0 +1,19 @@ +from django.shortcuts import render +from django.http import HttpResponse + +from .models import Greeting + +# Create your views here. +def index(request): + # return HttpResponse('Hello from Python!') + return render(request, "index.html") + + +def db(request): + + greeting = Greeting() + greeting.save() + + greetings = Greeting.objects.all() + + return render(request, "db.html", {"greetings": greetings}) diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..eb3ab8a --- /dev/null +++ b/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4af6a37 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +django +gunicorn +django-heroku \ No newline at end of file diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..2b301d5 --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-3.10.1