How to make a date Slug?

Asked

Viewed 62 times

0

I’m trying to add a Slug that converts the date to a tip that html accepts, but I don’t know how to make it work.

on the models.py part

class Por_dia(models.Model):
    pedidos = models.CharField(max_length=30, default='11')
    dia = models.DateTimeField()

and in the views part I set a fixed date only for testing but now I wish it could read the day the user select from a Slug.

def home4(request, data):

    data= '2018-06-11'
    usr= Pedidos.objects.filter(dia=data)
    context ={'lista_por_dia' : usr}
    return render(request,'por_dia.html', context)

But not without very well how to declare and make Sug become the variable "variable"

  • Hello! Edit the content of your question and add your code, what you’ve done so far, and your questions about what’s wrong with your code. I recommend touring through the help content: https://answall.com/tour - Greetings.

  • What you mean by " select user from a Slug"?

1 answer

0

You can use the function strptime library datetime. Your code would look something like:

from datetime import datetime
def home4(request, data):

    data= '2018-06-11'
    data = datetime.strptime(data, '%Y-%m-%d')
    usr= Pedidos.objects.filter(dia=data)
    context ={'lista_por_dia' : usr}
    return render(request,'por_dia.html', context)

Taking into account that date is a string that always comes in the format presented in this example, this is YYYY-MM-DD.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.