samedi 9 août 2014

python - Django admin de base de données, ne pas à jour correctement après admin entrée - Stack Overflow


I'm having an issue where if I delete a Product in the admin page all of the URLS created by the template that link to the remaining products don't work anymore and every page gives the error "No Image matches the given query."


For example, if there is only one product left the URL printed from the template should be .../products/1 but is instead showing up as ../products/3 which points to nothing. Furthering the confusion if I manually point to /products/1 I also get the same error


I'm guessing the problem is due to the foreign key relationship between Image and Product or how the db is using product_image_id but I can't figure it out. Please Help!


models


class Product(models.Model):
product_name = models.CharField(max_length=200)
product_type = models.CharField(max_length=200)
product_description = models.TextField()
def __unicode__(self):
return self.product_name

class Image(models.Model):
product_image = models.ForeignKey(Product)
image = models.ImageField(upload_to='image')

views


def productindex(request):
product_list = Product.objects.all()
context = {'product_list' : product_list}
return render(request, 'polls/products.html', context)

def productpage(request, product_image_id):
product = get_object_or_404(Product, pk=product_image_id)
image = get_object_or_404(Image, pk=product_image_id)
product_list = Product.objects.all()
return render(request, 'polls/productpage.html', {'product': product, 'image': image, 'product_list' : product_list})

urls


url(r'products/(?P<product_image_id>\d+)/$', views.productpage, name='productpage'),

template


   {% for product in product_list %}
<a href="{% url 'polls:productpage' product.id %}"><li class="procont"></li></a>
{% endfor %}



Based on jef79m's help I came to these conclusions which solved the problem


urls


    url(r'products/(?P<product_id>\d+)/$', views.productpage, name='productpage'),

views


def productpage(request, product_id):
product_list = Product.objects.all()
product = get_object_or_404(Product, pk=product_id)
image = product.image_set.all()
return render(request, 'polls/productpage.html', {'product': product, 'image': image, 'product_list' : product_list})



You should make sure you are referencing the correct objects in each section. For your product page, in the urls you should be passing in a product_id.


Then in your views, you retrieve that product like you have, but instead of getting an image using the pk, you should get product.image_set.all() to get all the images related to that product.


Double check the tutorial: https://docs.djangoproject.com/en/dev/intro/tutorial01/


In this case, "Question" is your product and "Choice" is your image.




Here is the final solution that AllTheTime arrived at.


urls


    url(r'products/(?P<product_id>\d+)/$', views.productpage, name='productpage'),

views


def productpage(request, product_id):
product_list = Product.objects.all()
product = get_object_or_404(Product, pk=product_id)
image = product.image_set.all()
return render(request, 'polls/productpage.html', {'product': product, 'image': image, 'product_list' : product_list})


I'm having an issue where if I delete a Product in the admin page all of the URLS created by the template that link to the remaining products don't work anymore and every page gives the error "No Image matches the given query."


For example, if there is only one product left the URL printed from the template should be .../products/1 but is instead showing up as ../products/3 which points to nothing. Furthering the confusion if I manually point to /products/1 I also get the same error


I'm guessing the problem is due to the foreign key relationship between Image and Product or how the db is using product_image_id but I can't figure it out. Please Help!


models


class Product(models.Model):
product_name = models.CharField(max_length=200)
product_type = models.CharField(max_length=200)
product_description = models.TextField()
def __unicode__(self):
return self.product_name

class Image(models.Model):
product_image = models.ForeignKey(Product)
image = models.ImageField(upload_to='image')

views


def productindex(request):
product_list = Product.objects.all()
context = {'product_list' : product_list}
return render(request, 'polls/products.html', context)

def productpage(request, product_image_id):
product = get_object_or_404(Product, pk=product_image_id)
image = get_object_or_404(Image, pk=product_image_id)
product_list = Product.objects.all()
return render(request, 'polls/productpage.html', {'product': product, 'image': image, 'product_list' : product_list})

urls


url(r'products/(?P<product_image_id>\d+)/$', views.productpage, name='productpage'),

template


   {% for product in product_list %}
<a href="{% url 'polls:productpage' product.id %}"><li class="procont"></li></a>
{% endfor %}


Based on jef79m's help I came to these conclusions which solved the problem


urls


    url(r'products/(?P<product_id>\d+)/$', views.productpage, name='productpage'),

views


def productpage(request, product_id):
product_list = Product.objects.all()
product = get_object_or_404(Product, pk=product_id)
image = product.image_set.all()
return render(request, 'polls/productpage.html', {'product': product, 'image': image, 'product_list' : product_list})


You should make sure you are referencing the correct objects in each section. For your product page, in the urls you should be passing in a product_id.


Then in your views, you retrieve that product like you have, but instead of getting an image using the pk, you should get product.image_set.all() to get all the images related to that product.


Double check the tutorial: https://docs.djangoproject.com/en/dev/intro/tutorial01/


In this case, "Question" is your product and "Choice" is your image.




Here is the final solution that AllTheTime arrived at.


urls


    url(r'products/(?P<product_id>\d+)/$', views.productpage, name='productpage'),

views


def productpage(request, product_id):
product_list = Product.objects.all()
product = get_object_or_404(Product, pk=product_id)
image = product.image_set.all()
return render(request, 'polls/productpage.html', {'product': product, 'image': image, 'product_list' : product_list})

0 commentaires:

Enregistrer un commentaire