Issue
i'm trying to read the value of a url and add it to the models in django.
Basically if i have the following link
my_site.com/special_event/123
How would i be able to catch the last part of the link the (123) and save it as an entry for my models. For context i am trying to do a raffle on my website, and when someone scans a QR code it will send them to my site and the end of the link will be a random number that i will use to raffle. So in this case a person's raffle ticket would be 123.
So far i have this code: the model:
class RaffleEntry(models.Model):
entry_number = models.IntegerField(blank=False)
def __str__(self):
return self.entry_number
And the view i tried doing it this way:
def add_entry(request, entry_number):
entry_number = RaffleEntry.objects.create()
return render(request, 'front/entry_success')
I also tried to add an INT parameter to the URL like this:
path('special_events/add_entry/<int:entry_number>', views.add_entry, name='special_events/add_entry>'),
Any help or even just point me in the right direction would be greatly appreciated. Thank you.
Solution
So to do what i wanted i just had to change my Views.py, here's the code:
def add_entry(request, entry_number):
RaffleEntry.instance = RaffleEntry.objects.create(entry_number=entry_number)
return render(request, 'front/entry_success.html', {'entry_number': entry_number})
This solved it.
Answered By - Rodrigo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.