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
0
Used termins
- Django views ⟶ In Django, views are a fundamental component that handle the logic of your web application. They process incoming web requests, interact with models (to retrieve or modify data), and return an HTTP response. In essence, views determine what data gets displayed and how it is presented by associating URLs with Python functions or classes.
- Django framework ⟶ Is a high-level, open-source web framework for building web applications using the Python programming language. It follows the model-view-template (MVT) architectural pattern and is designed to facilitate rapid development while promoting clean, pragmatic design.
- Django migrations ⟶ Are a way of propagating changes made to your Django models (which represent your database schema) into the actual database schema. They are a core part of Django's ORM (Object-Relational Mapping) system and help you manage the database schema in a versioned and structured manner.
- Website ⟶ Is defined as a collection of related web pages that are typically identified by a common domain name and published on at least one web server. Websites can serve various purposes and can include anything from personal blogs to business sites, e-commerce platforms, or informational resources.
- Django template ⟶ This is a text document marked up with a special syntax for inserting new code.
Related questions
- I can’t stand Django template language. Do I have to use it? I think this template engine is the best thing ever, but I know that choosing a template language runs close to religion. There’s nothing about Django that requires using the template language, so if you’re attached to Jinja2, Mako, or whatever it's ok.
- How can I see the raw SQL queries Django is running? Make sure your Django DEBUG setting is set to True. Then import connection from django.db. connection.queries is only available if DEBUG is True. It’s a list of dictionaries in order of query execution. Each dictionary has the sql and time property.
- If I make changes to a model, how do I update the database? Take a look at Django’s support for schema migrations. If you don’t mind clearing data, your project’s manage.py utility has a flush option to reset the database to the state it was in immediately after migrate was executed.