Django – extended URLs

Name of Innovation

Django – extended URLs

September 25, 2019 Uncategorized 0
mkdir 20221217
cd 20221217

conda activate myenv

django-admin startproject extendedurls

cd extendedurls

python manage.py startapp blogs

python manage.py startapp mycontacts

Register both the apps -
INSTALLED_APPS = [
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blogs',
 'mycontacts', 
]
python manage.py migrate
python manage.py createsuperuser

Please copy/paste following in extendedurls/urls.py

from django.contrib import admin from django.urls import path from django.conf.urls import include #, url from django.urls import include, re_path urlpatterns = [     path('admin/', admin.site.urls),     re_path('blogs.*/', include('blogs.urls')),     re_path('mycontacts.*/', include('mycontacts.urls')), ] Please copy paste following in mycontacts/urls.py from django.contrib import admin from django.urls import path from django.conf.urls import include #, url from django.urls import include, re_path from mycontacts import views urlpatterns = [     re_path(r'whoami.*', views.whoami, name='whoami'),     re_path(r'whoareyou.*', views.whoareyou, name='whoareyou'), ] Plese copy paste following in mycontacts/views.py from django.shortcuts import render # Create your views here. from django.http import HttpResponse from django.shortcuts import render import operator def whoareyou(request): return HttpResponse('Yes, I am the King!') def whoami(request): return HttpResponse('Ohh ok ! Me too - the King!') Please copy paste following in blogs/urls.py from django.contrib import admin from django.urls import path from django.conf.urls import include#, url from django.urls import include, re_path from blogs import views urlpatterns = [     re_path(r'foods.*', views.foods, name='foods'),     re_path(r'drinks.*', views.drinks, name='drinks'), ] Please copy paste following in blogs/views.py from django.shortcuts import render # Create your views here. from django.shortcuts import render    # Create your views here. from django.http import HttpResponse from django.shortcuts import render import operator def drinks(request): return HttpResponse('Drink 4L waters a day to stay healthy') def foods(request): return HttpResponse('Eat more green vegetables to stay healthy') Try the following URLS - http://127.0.0.1:8000/blogs-4-food/foods-which-is-healthy http://127.0.0.1:8000/blogs-4-drinks/drinks-for-me http://127.0.0.1:8000/mycontacts-in-india/whoami-please-tell-me http://127.0.0.1:8000/mycontacts-in-uk/whoareyou-please-tell-me