Most efficient way to create an Add Event button [Django / Python]?

An attempt was made to implement the "Visit" button. It's simple (or so I thought). A user with a profile clicks "Visit" and a checkmark appears. Can't get this to work on the Django / Python side. ie: Place this user who clicked "Attend" of the participant (list) within the event.

Template:

{% if is_attending %}
     <button class="btn" disabled="disabled">
         <i class="icon-ok-sign"></i> Attending
     </button>
{% else %}
     <form class="left" method="POST" action="/profile/event/{{ event.id }}/">
     {% csrf_token %}
          <input type="hidden" name="profile_id" value="user" />
          <button class="btn">
               Attending
          </button>
     </form>
{% endif %}

      

Models:

class Event(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=200)
    location = models.CharField(max_length=200)
    start_date = models.DateTimeField(auto_now=False, auto_now_add=False)
    end_date = models.DateTimeField(auto_now=False, auto_now_add=False)
    about = models.TextField(null=True, blank=True)
    attendees = models.ManyToManyField(Profile, null=True, blank=True)

      

View:

@login_required
def attending_event(request, event_id):
     event = get_object_or_404(Event, id=event_id)
     if request.method == 'POST':
         try:
             id = request.POST.get('profile_id')
             attendee = Profile.objects.get(id=id)
             relationship = Event.objects.create(attendees__user=attendee)
             is_attending = True
        except:
            pass 
        return HttpResponseRedirect('/profile/event/' + event_id + '/')
    else: 
        if not Event.objects.filter(attendees__user=request.user).exists():
            is_attending = False
    data = { 
        'is_attending': is_attending
        }
    return render_to_response('profiles/event.html', data, context_instance=RequestContext(request))

      

Perhaps there is something I am missing and cannot see what I am doing wrong. But if anyone can suggest some ideas / advice on how to do this; I would really appreciate it.

+3


source to share


1 answer


Just by giving you a hint, change it according to your needs

Template:

{% if is_attending %} 
     <button class="btn"> # this code will executes when is_attending is True
         <i class="icon-ok-sign"></i> Attending
     </button>
{% else %}
     <form class="left" method="POST" action="/profile/event/{{ event.id }}/"> # Always user reverse urls instead of Hard coded
     {% csrf_token %}
          <input type="hidden" name="profile_id" value="{{profile.id}}" />
          <button class="btn">
               Attending
          </button>
     </form>
{% endif %}

      



View:

@login_required
def event_profile(request, event_id)
    event = get_object_or_404(Event, id=event_id)
    if request.method == 'POST':
        try:
            id = request.POST.get('profile_id')
            attendee = Profile.objects.get(id=id)
            relationship = Event.objects.create(attendees__user=attendee, .... ) # set other variable you want
            is_attending = True
    else:
        # check in your event either your profile user is already attending or not? and set is_attending variable according to it
    data = { 
            'is_attending': is_attending,
             ....
            }
   return render_to_response('my_template.html',
                          data,
                          context_instance=RequestContext(request))

      

+2


source







All Articles