Django --start project without start project

It's a shallow knowledge of Django Newbie, but I'll write it as a memorandum.

Introduction

On the command line when creating a new project in Django

$ django-admin startproject project_name

I think that you often start a project by typing. This command will automatically create a project in Django 1.8 with a directory layout similar to the following:

project_name/
    manage.py
    project_name/
        __init__.py
        settings.py
        urls.py
        wsgi.py

From the beginning, it would be very helpful for Newbie to prepare from the beginning, but it seems that there is actually a way to create a project in the initial state of your choice without these automatically created files.

Now.

Now let's create a project. This time, I would like to output "Hello, World!" On the browser using one python file. Create a file called hello.py in a directory of your choice.

First, from view

First, let's create a view. That said, it ’s very easy,

hello.py


from django.http import HttpResponse

def index(request):
    return HttpResponse('Hello, World!')

Just type. It listens for a request and returns a string. This is what is originally described in view.py.

URL Patterns Next is the setting of urlpatterns. The regular expression pattern determines which view is returned for which url request. This time, only one string is displayed, so there is only one url pattern. This will also be added to hello.py.

hello.py


...
from django.conf.urls import url

urlpatterns = {
    url(r'^$', index),
}

settings Here, in the default project, the minimum required settings described in settings.py, such as ROOT_URLCONF and MIDDLEWARE_CLASSES, are added to hello.py.

hello.py


...
from django.conf import settings

settings.configure(
    DEBUG = True,
    SECRET_KEY = 'secretkey',
    ROOT_URLCONF = __name__,
    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    )
)

That's all the main code. How about that. It's a worryingly small description compared to the default project, but it works well as a Django project.

In order to start this project, import sys.

hello.py


import sys

...

if __name__ == '__main__':
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

that's all. At the command line $ python hello.py runserver

Enter. Then, the same character string as when starting from manage.py of the default project appears on the command line, and when accessing http://127.0.0.1:8000/, "Hello, World!" Is displayed as planned. I think.

What are you happy about

Now, what makes me happy when I create a project from scratch like this is that I can create a different project using the project I created as a template. For example, suppose you put the hello.py you just created in a directory like this:

hello/
    hello.py

In this case, hello is the project name. Now, let's create a new project based on this hello project.

In the same directory as the hello project, enter the following command. $ django-admin startproject hoge --template=hello Then, instead of Django's default project layout,

hoge/
    hello.py

The project starts with a layout like this. As long as you create a template, you can freely change the layout depending on the scale and functions of the project you are creating.

As mentioned above, I would like to post if there are any discoveries.

Recommended Posts

Django --start project without start project
Start a Django project
Start Django Tutorial 1
Django Project Baseline
Django project environment construction
[Django] Rename the project
Start today with Django
Shell to create django project
django project development environment construction
Deploy django project to heroku
A solution when you can't start project Django on Windows
Steps to create a Django project
Register your Django application in your project
Django
Deep learning to start without GPU
Start Django for the first time
Commands for creating a new django project
CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment
Django Project HTTPS Server Settings: Let's Encrypt
Django project structure customization (splitting and structuring)
[Python] Introduce UIKit3 to your Django project
Create a Django project and application in a Python virtual environment and start the server