I'm relatively new to Django. I am currently making a website for a small student association (approx. 100 people) and trying to make an event app on my website so that admins can create events, which users can choose to attend. I have successfully created the Models and used ListView and DetailView so that users can see which events are available.
I'm having a problem making a button on the event detail page so that users can say whether they're attending the event with the click of a button. I am currently only able to do this through the admin site, but I want users to sign up themselves.
As I understand I need to use some kind of a form, but I'm not sure how it is best to do this. It sounds simple, but I haven't been able to figure it out despite searching the web for a few days. As I say, I'm new to Django.
I have two models, one for the events and one for the registrations, which links the users to the events and has information on when the users registered.
Here are my files:
Models.py
import datetime
from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class Event(models.Model):
title = models.CharField('Title', max_length=140)
location = models.CharField('location', max_length=150)
info = models.TextField()
starts = models.DateTimeField('Starts')
ends = models.DateTimeField('Ends')
arrive_when = models.DateTimeField('Arrival time', null = True, blank = True)
arrive_where = models.CharField('Arrival location', null = True, max_length = 150, blank = True)
registration_starts = models.DateTimeField('Registration start')
registration_limit = models.IntegerField('Guest limit',
default=0,
choices=[(0, u"No limit")] + list(zip(range(1,100), range(1,100))))
class Meta:
verbose_name = "event"
verbose_name_plural = "events"
ordering = ['-starts']
def __str__(self):
if self.starts.date() != self.ends.date():
return u"%s, %s - %s" % (self.title,
self.starts.strftime("%a %H:%M"),
self.ends.strftime("%a %H:%M"))
else:
return u"%s, %s - %s" % (self.title,
self.starts.strftime("%H:%M"),
self.ends.strftime("%H:%M"))
def get_registrations(self):
return EventRegistration.objects.filter(event = self)
class EventRegistration(models.Model):
event = models.ForeignKey(Event,verbose_name='Event')
user = models.ForeignKey(User,verbose_name='Attendee')
time_registered = models.DateTimeField()
def __str__(self):
return self.user.username
class Meta:
verbose_name = 'Attendee for event'
verbose_name_plural = 'Attendees for events'
ordering = ['time_registered', ]
unique_together = ('event', 'user')
def save(self, *args, **kwargs):
if self.id is None and self.time_registered is None:
self.time_registered = datetime.datetime.now()
super(EventRegistration, self).save(*args, **kwargs)
views.py
from django.shortcuts import render, redirect
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
import datetime
from .models import Event, EventRegistration
# Create your views here.
class IndexView(generic.ListView):
template_name = 'events/index.html'
context_object_name = 'event_list'
def get_queryset(self):
return Event.objects.all()
class DetailView(generic.DetailView):
model = Event
template_name = 'events/detail.html'
urls.py
from django.conf.urls import url, include
from . import views
app_name = 'events'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail' ),
]
Can someone please help me make a button so that people can sign up for the events?