Menticolcito
Menticolcito

Reputation: 779

How to make a "beautiful" URL with Django-Python

I have this URL product of a form:

/capidahl?fecha=2014-06-23&hora1=00&minutos1=00&hora2=23&minutos2=59

I want to make it shorter, like capidahl/2014/06/24.

how can I rewrite the URL using Django?

Upvotes: 2

Views: 783

Answers (2)

PyReda
PyReda

Reputation: 65

here is an example about how to do it on model level (change it as you want):

class Article(models.Model):
    title = models.CharField( max_length = 255 )
    date_publish = models.DateTimeField(auto_now_add = True)
    def get_absolute_url(self):
        return 'capidahl/%s/%s/%s' %(self.date_publish.year, self.date_publish.strftime('%m'),  self.date_publish.strftime('%d'))

Upvotes: 0

GAEfan
GAEfan

Reputation: 11360

That is easily done in django. In urls.py, you specify the handler:

(r'^capidahl/(?P<fecha>[0-9-.]+)/*(?P<hora>[0-9]+)/*(?P<minutos>[0-9]+)', 'my_function')

Then, the function handles the variables:

def my_function(request, fecha, hora='12', minutos='00'):
    do something

Explained at: https://docs.djangoproject.com/en/dev/topics/http/urls/

Upvotes: 3

Related Questions