создать новую модель Series которая будет привязана к Model
и выводить все серии по какой либо модели
по url - /brand//model//
Не понял в чем у меня ошибка, перехожу по директории /brand/1/model/1/ Но ничего нету, хотя она есть
Выдает Ошибку
Page not found (404)
Models
from django.db import models
from brands.models import Brand
POST_TYPE_CHOICES = (
('Mechanics', 'Mechanics'),
('Avomatick', 'Avomatick')
)
class Model(models.Model):
name = models.CharField(max_length=20)
engine = models.TextField(max_length=100)
hp = models.IntegerField(null=True)
nm = models.IntegerField(null=True)
KPP = models.CharField(choices=POST_TYPE_CHOICES, max_length=30)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, null=False)
image = models.ImageField(upload_to='model_images', null=True, blank=True)
def __str__(self):
return f'{self.name}'
class Series(models.Model):
series = models.CharField(max_length=100, null=True, blank=True)
model = models.ForeignKey(Model, on_delete=models.CASCADE, null=False)
image = models.ImageField(upload_to='model_images', null=True, blank=True)
def __str__(self):
return f'{self.series}'
views
from django.shortcuts import render
from models.models import Model
from brands.models import Brand
# Create your views here.
def ModelView(request):
posts = Model.objects.all()
data = {
'posts': posts
}
return render(request, 'brand.html', context=data)
def model_detail(request, id):
post = Model.objects.get(id=id)
data = {
'post': post
}
return render(request, 'detail.html', context=data)
def SeriesView(request, brand_id, model_id):
brand = Brand.objects.get(id=brand_id)
model = Model.objects.get(id=model_id)
# seriess = Model.objects.filter(model_id=model)
data = {
# 'series': seriess,
'brand': brand,
'model': model
}
return render(request, 'series.html', context=data)
urls
"""Cars URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from brands.views import BrandView
from models.views import ModelView
from models.views import model_detail
from django.conf import settings
from django.conf.urls.static import static
from models.views import SeriesView
urlpatterns = [
path('admin/', admin.site.urls),
path("", BrandView),
path('post/<int:id>/', model_detail),
path('brand/<int:brand_id>model/<int:model_id>/', SeriesView),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
HTML
{% extends 'main.html'%}
{% block posts %}
<div style="display: flex; justify-content: space-between">
{% for post in posts %}
<div style="text-align: center; height: 150px; width: 300px;">
<img src="/media/{{ post.image}}" alt="" style="width: 200px">
{% if post.stars > 2 %}
<h3 style="color: green">{{ post.series }}</h3>
{% else %}
<h3 style="color: red">{{ post.series }}</h3>
{% endif %}
<a href="post/{{series.id}}">More</a>
</div>
{% endfor %}
</div>
{% endblock %}