以下代码将登录用户分配给对象,如何更改它以将下拉菜单中的选定用户分配给对象?
<form method='POST' action="{% url 'post:car' post.id %}">
{% csrf_token %}
<label for="select1"></label>
<select class="mdb-select md-form" id="select1" onChange="form.submit();">
<option value="" disabled selected>Assign</option>
{% for user in current_users %}
<option value="{{ user.id }}"> {{ user }} </option>
{% endfor %}
</select>
</form>
上面的目标是将选定的分配{{user.id}}给{{post.id}}
该urls.py是
path('post/<int:tag>', views.assigned_post, name='car'),
在current_users已经通过注射从已经实现views.py使用 context['current_users'] = ProjectUser.objects.all()
该views.py是
def assigned_post(request, tag):
as_curr = get_object_or_404(Post, id=tag)
if request.method == 'POST':
if as_curr.assigned_to:
as_curr.assigned_to = None
as_curr.save()
else:
as_curr.assigned_to = request.user # < -- Assign to drop down user instead of current user
as_curr.save()
return HttpResponseRedirect(reverse('post:tasks'))
该models.py是
class Post(models.Model):
assigned_to = models.ForeignKey(ProjectUser, related_name="assigned_user", blank=True, null=True,
on_delete=models.CASCADE)
我试过的:
as_curr.assigned_to = request.POST.get('user_id')
但意见正在恢复None的request.POST.get('user_id')
杨__羊羊
德玛西亚99
相关分类