I am trying to pass both the user.id
and sample.id
to a function in the views.
Here is my urls.py (url only accepts sample.id
)
path('<int:tag>', views.task, name='task'),
Here is my views.py (views.py is looking for user.id
but cannot find it)
def task(request, tag):
task_user = get_object_or_404(Team, id=tag)
task_user.assigned_to = ProjectUser.objects.get(id=request.POST.get('user_id'))
task_user.save()
return HttpResponseRedirect(reverse('someOtherPage'))
Here is my html template
<form method='POST' action="{% url 'task' object.id %}">
{% csrf_token %}
<label for="select1"></label>
<select name="user_id" class="mdb-select md-form" id="select1" onChange="form.submit();">
{% for user in usernames %}
<option value="{{ user.id }}"> {{ user }} </option>
{% endfor %}
</select>
</form>
Here is my tests.py
def someTest(self):
self.client.login(username='jack', password='123')
user1 = auth.get_user(self.client)
assert user1.is_authenticated
sample = Team.objects.create(sampleField='SampleName')
request = self.client.post(reverse('analyst_view_app:task_analyst', kwargs={'tag': sample.id}))
The error message is:
SomeApp.SomeModel.ProjectUser.DoesNotExist: ProjectUser matching query does not exist.
How can I pass the user1.id
from my test to the function task
-> user_id
?
You're not passing any POST data in your test. You need to send a dictionary representing the data from the form, as the second argument to the client.post()
call.
request = self.client.post(
reverse('analyst_view_app:task_analyst', kwargs={'tag': sample.id}),
{'user_id': user1.id}
)
(Note though, if your view is supposed to be always getting the logged-in user, there doesn't seem to be any point in the select box at all. If not, then you should change your test; perhaps you don't need to log in at all there?)