[PYTHON] Create a homepage with django

I've made it many times until now, and then remade it again and again, and every time I googled various places, I wondered if I should leave a memorandum.

By the way, the content is almost https://qiita.com/noraricl/items/08937a508a2abecc7179 It will be as it is written. (I wanted to make a super-easy minimum page, so it was very helpful ...)

Environmental setting

I have created a virtual environment called rate-site from version 3.6.1 with pyenv and virtualenv, but anyone other than me can skip this.

mkdir rate_site
cd rate_site
pyenv virtualenv 3.6.1 rate-site
pyenv local rate-site

Create the following requirements.txt.

dj-database-url==0.5.0
dj-static==0.0.6
Django==2.2.6
django-heroku==0.3.1
gunicorn==20.0.0
mysqlclient==1.4.4
psycopg2==2.8.4
PyMySQL==0.9.3
python-dateutil==2.8.1
pytz==2019.3
six==1.13.0
sqlparse==0.3.0
static3==0.7.0
whitenoise==4.1.4

And

pip install -r requirements.txt

Install what you need at.

Project and application creation

mkdir rate_site #Now rate_Make another one in the site
cd rate_site
django-admin startproject rate_site .
python manage.py startapp rate_hp

Edit configuration file

rate_site/settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rate_hp', #add to
]
(abridgement)
LANGUAGE_CODE = 'ja' #Change
TIME_ZONE = 'Asia/Tokyo' #Change
(abridgement)
USE_TZ =  False #Change

migrate

python manage.py migrate

Created by administrator user

python manage.py createsuperuser

Start confirmation

python manage.py runserver

with this, http://127.0.0.1:8000/

When you visit, django displays the default page.

Make rate_hp accessible

rate_site/urls.py


from django.urls import path, include #add to

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('rate_hp.urls')), #add to
]

HTML file creation

In the directory where rate_site and rate_hp are located, Create a templates directory and

templates/base.html


<html>
    <head>
        <title>home page</title>
    </head>
    <body>
        <p>This is base.It is html.</p>

    {% block main_containts %}
    {% endblock %}

        <p>This is base.It is html.</p>
    </body>
</html>

templates/index.html


{% extends "base.html" %}
{% block main_containts %}
{% load static %}

  <main>
Here is the index.It is html.
  </main>

{% endblock %}

Edit the file in rate_hp.

rate_hp/views.py


from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader

def index(request):
    template = loader.get_template('index.html')
    context = {}
    return HttpResponse(template.render(context, request))

ʻUrls.py` does not exist, so create a new one

rate_hp/urls.py


from django.urls import path
from . import views

app_name = 'rate_hp' #django2.Namespace definition needed from 0
urlpatterns = [
    path('', views.index, name='index'),
]

Make django load the templates you just created.

rate_site/settings.py


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],  #add to
        'APP_DIRS': True,

Here too python manage.py runserver

To run http://127.0.0.1:8000/ To access.

Once completed

In the future, it is assumed that we will continue to add new applications to the home here so that they can be accessed.

Recommended Posts

Create a homepage with django
Create a file uploader with Django
Create a Django schedule
Create a dashboard for Network devices with Django!
Create a one-file hello world application with django
Create an API with Django
Create a Django login screen
Create a heatmap with pyqtgraph
Create a directory with python
Create a Todo app with Django REST Framework + Angular
I tried to create a table only with Django
Create a Todo app with the Django REST framework
Create a Todo app with Django ③ Create a task list page
Create a Todo app with Django ⑤ Create a task editing function
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Steps to create a Django project
Deploy a Django application with Docker
Create a virtual environment with Python!
Django Tips-Create a ranking site with Django-
Build a web application with Django
Make a filter with a django template
Create a poisson stepper with numpy.random
Create a LINE Bot in Django
Create a web API that can deliver images with Django
Create a Todo app with Django ① Build an environment with Docker
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Create a Python function decorator with Class
Create RESTful APIs with Django Rest Framework
A simple RSS reader made with Django
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Create a temporary file with django as a zip file and return it
Internationalization with django
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Create a GUI app with Python's Tkinter
Rails users try to create a simple blog engine with Django
Create a large text file with shellscript
Create a star system with Blender 2.80 script
Create a virtual environment with Python_Mac version
Creating a login screen with Django allauth
Create a VM with a YAML file (KVM)
Create an update screen with Django Updateview
Create a simple web app with flask
Create a word frequency counter with Python 3.4
Create your first app with Django startproject
CRUD with Django
A note on enabling PostgreSQL with Django
Create a Connecting Nearest Neighbor with NetworkX
Create a web service with Docker + Flask
Create a private repository with AWS CodeArtifact
Create a car meter with raspberry pi
Create a devilish picture with Blender scripts
Create a matrix with PythonGUI (text box)
Create a REST API to operate dynamodb with the Django REST Framework
I made a WEB application with Django
Create a graph with borders removed with matplotlib
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
[Can be done in 10 minutes] Create a local website quickly with Django