Create and configure a new app for Django
19.07.2024
15.04.2025
1 minute
71
0
0
0
0
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
Comments
(0)
Send
It's empty now. Be the first (oοΎvοΎ)γ
Other
Similar articles
Used termins
- Website builder βΆ It is an app or web service, with a collection of ready for use templates and tools, for constructing a website.
- Django model βΆ Is a database manager used in the Django framework. Implemented via classes and inheritance in python.
- Python programming language βΆ It is interpreted, build upon object-oriented approach, with dynamic semantics, and yes it s high-level programming language. Actively using for rapid development, scripting and gluing part of existing apps.
- 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.
- 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.
Related questions
- How do I use image and file fields? Using a FileField or an ImageField in a model takes a few steps: 1) In your settings file, youβll need to define MEDIA_ROOT 2) Add the FileField or ImageField to your model 3) All that will be stored in your database is a path to the file (relative to MEDIA_ROOT)
- How do I make a variable available to all my templates? Sometimes your templates all need the same thing. A common example would be dynamically generated menus. At first glance, it seems logical to add a common dictionary to the template context. The best way to do this in Django is to use a RequestContext
- 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.
- How do I limit admin access so that objects can only be edited by the users who created them? The ModelAdmin class also provides customization hooks that allow you to control the visibility and editability of objects in the admin. Using the same trick of extracting the user from the request, the get_queryset() and has_change_permission() can be used to control the visibility and editability of objects in the admin.