python - Genering dedicated urls with slugs -
i asked in past time how generate url similar caracterist starting id said me improve utilize slugs that. on time wanna generate dinamics urls slugs. objective obtain result:
i have 5 products named cards in models.py (ysera, neltharion, nozdormu, alexstrasza, malygos). need respective url of each of products:
localhost:8000/card/ysera
localhost:8000/card/neltharion
localhost:8000/card/nozdormu ... etc.
i seek generate these urls dont know if made applying of commands, either don't know how can specify id card main name of card (ysera, neltharion...). trying follow reply posted here in community little blind , "reconfiguration":
here views.py:
from django.shortcuts import render_to_response django.template import requestcontext dracoin.apps.synopticup.models import card dracoin.apps.home.forms import contactform,loginform django.core.mail import emailmultialternatives django.contrib.auth import login,logout,authenticate django.http import httpresponseredirect def shop(request): tarj = card.objects.filter(status=true) ctx = {'tarjetas':tarj} homecoming render_to_response('home/shop.html',ctx,context_instance=requestcontext(request)) def singlecard(request, slug, id): try: tarj = card.objects.get(slug=slug, id=id_tarj) except objectdoesnotexist: tarj = get_object_or_404(card, id=id_tarj) homecoming render_to_response('home/singlecard.html',ctx,context_instance=requestcontext(request))
my urls.py (i have urls.py app , main urls.py):
url(r'^card/(?p<slug>[-\w]+)/(?p<id_tarj>\d+)/$','dracoin.apps.home.views.singlecard',name='vista_single_card'),
my models.py:
class card(models.model): nombre = models.charfield(max_length=100) descripcion = models.textfield(max_length=300) status = models.booleanfield(default=true) def __unicode__(self): homecoming self.nombre
my mutual template cards:
{% 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 slug building in views.py find, i'm convinced urls.py bad don't know how build it?
please excuse me if edit own question prolong it, i'm trying larn django side , have many gaps in learning
apologizeme in advance extensive question , if overlook something.
thanks!!
this line:
tarj = card.objects.get(slug=slug, id=id_tarj)
tries load card
object id
field set id_tarj
, slug
field set slug
. model not have field named slug
. need add together one.
a candidate slugfield
- https://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield
you need create sure slug field contains proper value in each case.
python django django-views url-routing slug
No comments:
Post a Comment