创建Attending Event选项按钮[Django / Python]的最有效方法?

时间:2012-03-07 18:05:49

标签: python django django-templates django-views

尝试实施“Attending”按钮。这很简单(或者我认为)。具有个人资料的用户单击“出席”,并出现复选标记。无法让这个在Django / Python方面工作。即:将点击“出席”的用户放入活动中的与会者(列表)。

模板:

{% 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 %}

型号:

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)

查看:

@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))

也许有一些我缺少的东西,看不出它是什么我做错了。但是,如果有人能提供一些有关如何完成此任务的见解/建议;我真的很感激。

1 个答案:

答案 0 :(得分:2)

只是给你提示,根据你的需要改变它

模板:

{% 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 %}

查看:

@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))