Question

I'm just starting off with python so bear with me.

Lets say this is my model:

class Event(models.Model):
    name = models.CharField(max_length=100, verbose_name=_('name'))
    location = models.CharField(max_length=200, verbose_name=_('location'))
    start_time = models.DateTimeField(verbose_name=_('start time'))
    end_time = models.DateTimeField(verbose_name=_('end time'))
    sales_start = models.DateField(verbose_name=_('sales start'))
    sales_end = models.DateField(verbose_name=_('sales end'))
    event_active = models.BooleanField(verbose_name=_('event active'))
    price = models.IntegerField(verbose_name=_('price'))
    maximum = models.IntegerField(verbose_name=_('maximum'))
    information = models.CharField(max_length=500, verbose_name=_('information'))
    logo = models.ImageField(verbose_name=_('logo'), blank=True, upload_to='img/%Y/%m/%d')

What I would like to accomplish is make a single page and fill it with data from an 'event', I would like to get this event by getting the Id from the url.

I'm not sure on how to do this as I don't understand certain parts of the documentation as I'm not a native English speaker.

Was it helpful?

Solution

I think this is what you are trying to do:

# urls.py
# note 'event_id' will be passed as argument to 'show_event' view.
url(r'events/(?P<event_id>\d+)/$', 'show_event', name='show_event')


# views.py
def show_event(request, event_id):
    ...
    # this will return a 404 response is case event with given id is not found
    event = get_object_or_404(Event, id=event_id)
    ...
    return render(request, 'template.html', {'event': event})


# template.html
<h1>Welcome to the event {{event.name}}</h1>

You will use URLs in this way: yourdomain.com/events/123. That would pull 123 event id from URL and render template.html sending the proper event object in the template context, so you can render it as you want.

OTHER TIPS

So your urls.py will need to be designed to except a regular expression of anything your page name could be. In the example provided by Martin you can also have a regular expression match text instead of just a number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top