Create and configure a new app for Django

Create a base Django project

Commands to create a Django app

We will start a new app, App1.

./manage.py startapp App1
In settings.py, register a new app.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'App1.apps.App1Config',
]

Commands to configure URLs

Connect the app’s URLs to project.

cd Project1/App1

touch urls.py
In a file urls.py

from django.urls import path
from .views import some

urlpatterns = [
    path('app/', some, name='some')
]
In a file views.py add:

def some(request):
    return render(request, 'App1/yourTemplate.html')
Create directories in Project1/App1

mkdir templates
mkdir templates/App1
Create an empty template.

touch template/App1/yourTemplate.html
Only one thing left, connect URLs in a file Project1/Project1/urls.py.

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns += [
        path('', include('App1.urls')),
    ]

Commands to configure django model (optional)

In a file Project1/App1/models.py, create either your own model or my testing one:

from django.db import models

class AppModel(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField()

    def _str_(self):
        return self.title
Let’s register our model in Project1/App1/admin.py for access via the admin panel on the website.

from django.contrib import admin
from .models import  AppModel

class AppModelAdmin(admin.ModelAdmin):
    list_display = ('title', 'description')

admin.site.register(AppModel, AppModelAdmin)
Applying migrations:

./manage.py makemigrations
./manage.py migrate
If you want to use default Django admin, you need to create a superuser. Jusk like that:

./manage.py createsuperuser

heart
0
3 connected dots
0

Used termins

Related questions