Задать вопрос
@Vova135798

Как настроить переход по slug?

Помогите пожалуйста настроить переход на страницы по slug, созданные с помощью detailview

models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Customer(models.Model):
	user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
	name = models.CharField(max_length=200, null=True)
	email = models.CharField(max_length=200)

	def __str__(self):
		return self.name


class Product(models.Model):
	name = models.CharField(max_length=200)
	price = models.FloatField()
	digital = models.BooleanField(default=False,null=True, blank=True)
	image = models.ImageField(null=True, blank=True)
	slug = models.SlugField(unique=True)

	def __str__(self):
		return self.name

	def get_absolute_url(self):
		return reverse(args=[self.slug])

	@property
	def imageURL(self):
		try:
			url = self.image.url
		except:
			url = ''
		return url

urls.py

from django.urls import path

from .views import *

urlpatterns = [
	#Leave as empty string for base url
	path('', store, name="store"),
	path('cart/', cart, name="cart"),
	path('checkout/', checkout, name="checkout"),
	path('<slug:slug>/', Product_det.as_view(), name='prod'),
	path('update_item/', updateItem, name="update_item"),
	path('process_order/', processOrder, name="process_order"),

]

views.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.http import JsonResponse
import json
import datetime
from .models import * 
from .utils import cookieCart, cartData, guestOrder

def store(request):
	data = cartData(request)

	cartItems = data['cartItems']
	order = data['order']
	items = data['items']

	products = Product.objects.all()
	context = {'products':products, 'cartItems':cartItems}
	return render(request, 'store/store.html', context)


class Product_det(DetailView):
	


	model = Product
	template_name = 'store/prod.html'
	context_object_name = 'product'

{% extends 'store/main.html' %}
{% load static %}
{% block content %}
	<div class="row">
		{% for product in products %}
		<div class="col-lg-4">
			<img class="thumbnail" src="{{product.imageURL}}">
			<div class="box-element product">
				<h6><strong>{{product.name}}</strong></h6>
				<hr>

				<button data-product="{{product.id}}" data-action="add" class="btn btn-outline-secondary add-btn update-cart">Add to Cart</button>
				
				<a class="btn btn-outline-success" href="{% url 'prod' %}">View</a>
				<h4 style="display: inline-block; float: right"><strong>${{product.price}}</strong></h4>

			</div>
		</div>
		{% endfor %}
	</div>

{% endblock content %}
  • Вопрос задан
  • 206 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ответы на вопрос 1
@YuriyCherniy
def get_absolute_url(self):
    return reverse('prod', args=[self.slug])
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы