Как вывести все модели автомобилей в Django? Чтобы по запросу
127.0.0.1:8000/cars_brand/bmw выдавало все модели автомобиля бмв
# Корневая папка urls:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('cars_brand/', include("car_store.urls")),
]
# Папка urls созданная мной:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index),
path("<cars_name>", views.get_all_cars_name),
]
# views
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseNotFound
# Create your views here.
cars_brand_dict = {
"audi": "ссылка на audi",
"bmw": "ссылка на bmw",
"volkswagen": "ссылка на volkswagen",
}
types = {
"audi": ["Audi_RS_2_Avant", "Audi_A8_W12", "Audi_TTS"],
"bmw": ["BMW_M135i_xDrive", "BMW_M240i_xDrive_Coupe", "BMW_M3_Competition"],
"volkswagen": ["Volkswagen_Caddy", "Volkswagen_Multivan", "Volkswagen_Crafter_Pritsche"],
}
def index(request):
cars_brand = list(cars_brand_dict)
li_elements = ""
for brand in cars_brand:
li_elements += f"<li> <a href={brand}>{brand.title()}</a> </li>"
response = f"""
<ul>
{li_elements}
</ul>
"""
return HttpResponse(response)
def get_all_cars_name(request):
cars_name = list(types)
li_elements = ""
for name in cars_name:
li_elements += f"<li> <a href={name}>{name.title()}</a> </li>"
response = f"""
<ul>
{li_elements}
</ul>
"""
return HttpResponse(response)