gpt4 book ai didi

python - 创建参加事件选项按钮的最有效方法[Django/Python]?

转载 作者:行者123 更新时间:2023-11-28 17:51:08 25 4
gpt4 key购买 nike

尝试实现“参加”按钮。这很简单(或者我认为如此)。具有个人资料的用户单击“参加”,然后会出现一个复选标记。不能完全让它在 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))

也许我遗漏了什么,看不出我做错了什么。但是,如果有人可以就如何完成这项工作提供一些见解/建议;我真的很感激。

最佳答案

只是提示一下,根据自己的需要改

模板:

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

关于python - 创建参加事件选项按钮的最有效方法[Django/Python]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com