Django URL settings

Introduction

Here, we will explain the settings related to the URL of django.

Project urls.py settings

First, edit ʻurls.py` under the project directory as follows.

Project name/urls.py


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


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Application name.urls')),
]

Where it says ʻadmin /, it shows the URL to the administration screen. For other pages, ʻinclude is used to mean that it is described in ʻurls.py` under the application directory.

Application urls.py settings

Also create ʻurls.pyunder the application directory. Basically, describe thepath function in the form of path (URL, view function (or class), name = name when back-referencing) `.

When using a class-based view

Application name/urls.py


from django.urls import path
from . import views


app_name =Application name

urlpatterns = [
    path('list/', views.SampleList.as_view(), name='app_list'),
    path('create/', views.SampleCreate.as_view(), name='app_create'),
    path('detail/<int:pk>', views.SampleDetail.as_view(), name='app_detail'),
    path('update/<int:pk>', views.SampleUpdate.as_view(), name='app_update'),
    path('delete/<int:pk>', views.SampleDelete.as_view(), name='app_delete'),
]

If you want to specify view in the class, write the ʻas_view` method after the class name.

When using a function-based view

Application name/urls.py


from django.urls import path
from . import views


app_name =Application name

urlpatterns = [
    path('list/', views.list_func, name='app_list'),
    path('create/', views.create_func, name='app_create'),
    path('detail/<int:pk>', views.detail_func, name='app_detail'),
    path('update/<int:pk>', views.update_func, name='app_update'),
    path('delete/<int:pk>', views.delete_func, name='app_delete'),
]

Summary

Here, I explained the URL related settings of django. Next time, I'll cover templates.

Recommended Posts

Django URL settings
django default settings
Django + MySQL settings
Django command completion settings
Django
DEBUG settings when using Django
django timezone and language settings
Django static file (static) related settings summary
Django note 4
DJango Memo: From the beginning (model settings)
Django memorandum
Django installation
WEB application development using Django [Initial settings]
samba settings
Django test
Django Note 5
DB settings when using Django + SQLAlchemy + Alembic
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
Django search
Install Django
Django Project HTTPS Server Settings: Let's Encrypt
Django: References
VIM settings
[Django] Settings for sending emails using postfix
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
Designing URL schemes and creating templates in Django
Specify the view URL in your Django template
[Django3] Environment construction and various settings summary [Python3]
Create initial settings and staff apps in Django
[Django] About static file related settings (images, videos)
Image URL is blank in GAE & GCS & Django
DJango Memo: From the beginning (Error screen settings)
[Django] About static file related settings (css, js)