You project doesn't have to have multiple apps. Instead you can use the project package that the startproject command created for you. By making a few changes, we can make it work like an app that was added with the startapp command.

We need to:

  • Add a configuration class.
  • Reference that class in the settings.py file INSTALLED_APPS list.
  • Create a migrations folder with a file __init__.py in it (for model migrations to work).
  • Add whatever you need for the project (views, models, urls etc).

Setup

Setup a new project using these instructions:

Django - PyCharm Project

OR do this:

py -m venv venv
venv\Scripts\activate.bat
pip install django
django-admin startproject mysite .

MysiteConfig

Create a file called apps.py in the mysite directory and add these lines to it:

from django.apps import AppConfig

class MysiteConfig(AppConfig):
    name = 'mysite'

Edit the settings.py file and add the MysiteConfig class to the INSTALLED_APPS list:

INSTALLED_APPS = [
    # ...
    'django.contrib.staticfiles',
    # HERE
    'mysite.apps.MysiteConfig'
]

Blog model

Create a file called models.py in the mysite directory and add the following lines to it:

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=255, 
                             default="")

Create a new directory called migrations in the mysite directory and add a file called __init__.py in it. The __init__.py file makes Python treat this directory as a module:

Run migrations and create a superuser:

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

Create a blog post and run the development server:

python manage.py shell
from mysite.models import Blog
Blog.objects.create(title="test")
exit()
python manage.py runserver

Create a file called admin.py in the mysite directory and add these lines to it:

from django.contrib import admin
from .models import Blog

admin.site.register(Blog)

Restart the development server and visit the admin site (http://127.0.0.1:8000/admin/mysite/blog/) to see the new blog item:

Views

Create a file called views.py in the mysite folder and add the following lines to it:

from django.http import HttpResponse

def home(request):
    html = "<h1>%s</h1>" % "Hello"
    return HttpResponse(html)

URLS

Edit the urls.py file and add the following path to it:

from django.contrib import admin
from django.urls import path

# HERE
from .views import home

urlpatterns = [
    # HERE
    path('', home, name="home"),
    path('admin/', admin.site.urls),
]

Visit the home page and you should see "Hello":

View templates

Create a new file called index.html in the templates\mysite directory (you have to create the folder structure) and add these lines to it:

<h1>Hello from template!</h1>

Edit the views.py file and make the following changes to the home view:

# from django.http import HttpResponse
from django.shortcuts import render

def home(request):
    # html = "<h1>%s</h1>" % "Hello"
    # return HttpResponse(html)
    return render(request, 'mysite/index.html')

Visit the home page and you should see the "Hello from template!" text:

Related