0

I'm trying to obtain dedicated urls of some products that I have in my shop.html page. I have five products that I named "cards": (Ysera, Neltharion, Nozdormu, Alexstrasza, Malygos). Each card should have a dedicated url (localhost:8000/card/1/, localhost:8000/card/2/, etc). but instead of obtaining that url, django launch me that message:

DoesNotExist at /card/1/ card matching query does not exist.

I imported properly the class model "card" in my views.py, in fact I am justly using card in a filter function to obtain all products in shop.html. please look my views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext
from dracoin.apps.synopticup.models import card
from dracoin.apps.home.forms import ContactForm,LoginForm
from django.core.mail import EmailMultiAlternatives

from django.contrib.auth import login,logout,authenticate
from django.http import HttpResponseRedirect    



def index(request):
    return render_to_response('home/index.html',context_instance=RequestContext(request))

def landing(request):
    return render_to_response('home/landing.html',context_instance=RequestContext(request))

def shop(request):
    tarj = card.objects.filter(status=True)
    ctx = {'tarjetas':tarj}
    return render_to_response('home/shop.html',ctx,context_instance=RequestContext(request))

def singleCard(request,id_tarj):    
    tarj = card.objects.get(id=id_tarj) 
    ctx = {'card':tarj}
    return render_to_response('home/singleCard.html',ctx,context_instance=RequestContext(request))

here my urls.py:

url(r'^card/(?P<id_tarj>.*)/$','dracoin.apps.home.views.singleCard',name='vista_single_card'),

My imported model:

class card(models.Model):
    nombre = models.CharField(max_length=100)
    descripcion = models.TextField(max_length=300)
    status = models.BooleanField(default=True)

    def __unicode__(self):
        return self.nombre

My singleCard.html:

{% extends 'base.html' %}
{% block title %} Tarjeta {{card.nombre}} {% endblock %}
{% block content %}

<h1>{{ card.nombre }}</h1><br> 
<p> {{ card.descripcion }}</p>        

{% endblock %}

I don't know if I have a wrong refering "card" class. But I try to apply other answers in this forum. For example:

In Django, how do I objects.get, but return None when nothing is found?

matching query does not exist Error in Django

Django error - matching query does not exist

I don't know if I commit a mistake applying these solutions. Including I try:

tarj = card.objects.filter(id=id_tarj)

Using this I obtain a blank page of my website...

apologizeme in advance my extensive question and if I overlook something.

Thanks!!


Answering to wolendranh I have an urls.py by app and the main urls.py.

Recently I'm learning django by my side and I can't understand how I can define my own consistent identifier in this case.

if it is still useful I put here a traceback generated with the error:

Environment:


Request Method: GET
Request URL: http://localhost:8000/card/1/

Django Version: 1.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'dracoin.apps.synopticup',
 'dracoin.apps.home')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/draicore/project/multilevel/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/draicore/project/dracoin/dracoin/apps/home/views.py" in singleCard
  24.       tarj = card.objects.get(id=id_tarj) 
File "/home/draicore/project/multilevel/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  92.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/draicore/project/multilevel/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  357.                 self.model._meta.object_name)

Exception Type: DoesNotExist at /card/1/
Exception Value: card matching query does not exist.

excuse me for prolong this question.

Community
  • 1
  • 1
Jhonny
  • 145
  • 1
  • 12
  • 1
    this means you dont have a card object with id 1 in your database. open a shell, and see what are valid ids of objects in the database – karthikr Oct 13 '14 at 14:33
  • also try setting errors to be displayed on your webserver, will help you a lot - alternatively check the apache? error logs – Martin B. Oct 13 '14 at 14:43

2 Answers2

0

I have few things to clarify: 1. Do you have a single urls.py file in your project? or separate for every app. If you have a separate like "your_project/card/urls" and it is included into main urls.py you should NOT use "card/" in your url. Because Django already knows that request is for that app.

r'^card/(?P<id_tarj>.*)/$' -> r'^(?P<id_tarj>.*)/$'
  1. If it is in main urls.py try to replace:

    r'^card/(?P<id_tarj>.*)/$' 
    

    to

    r'^card/(?P\d+))/$'

P.s.: I don't have anough reputation for comments, so I added an answer. Sorry.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
wolendranh
  • 4,202
  • 1
  • 28
  • 37
0

As karthikr says in the comment, you don't have a card with id=1.

This is probably because you previously deleted and recreated a card. The id is an autoincrement field, which means the database does not reuse IDs that have been deleted. If you want your item to have a consistent identifier which you can always use to query it in the URL, you should probably define that as an explicit IntegerField (using another name than id), and query against that instead. Even better, use a slug rather than a numeric ID.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895