'How to get rid of TypeError at /task_create/, quote_from_bytes() expected bytes
I am on django 4.0.4 and have a benign but annoying problem with the above error code.
Previous responses to similar questions don't help me.
When I backspace, (an incovenience a user may not think of) everything has actually gone well, the list and detail views are correctly updated.
The create form is however as it was as if it was not saved, and the view doesn't change to the detail view as suggested by the get_absolute_url, which may lead one to process it again.
The code from previous models is exactly the same where it matters. An example is the TaskCreate view which is giving the problem and the ObjectiveCreate view just before it which works as expected.
In the views, I find the user and his/her entity (even though there is one entity), and process that user as a supervisor1 and also automatically process the entity.
The error finds fault with the TaskCreate view's "return super().form_valid(form)" but not with that in the ObjectiveCreate view.
Here is the code for the two views
class ObjectiveCreate(LoginRequiredMixin, CreateView):
model = Objective
form_class = ObjectiveCreateForm
template_name = "internalcontrol/objective_create_form.html"
def form_valid(self, form):
user = self.request.user
profile = Profile.objects.get(user=user)
entity = profile.entity
new_profile = Profile.objects.get(user=user, entity=entity)
form.instance.supervisor1 = new_profile
form.instance.entity = entity
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(ObjectiveCreate, self).get_form_kwargs()
kwargs["request"] = self.request
return kwargs
class TaskCreate(LoginRequiredMixin, CreateView):
model = Task
form_class = TaskCreateForm
template_name = "internalcontrol/task_create_form.html"
def form_valid(self, form):
user = self.request.user
profile = Profile.objects.get(user=user)
entity = profile.entity
new_profile = Profile.objects.get(user=user, entity=entity)
form.instance.supervisor1 = new_profile
form.instance.entity = entity
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(TaskCreate, self).get_form_kwargs()
kwargs["request"] = self.request
return kwargs
The respective get_absolute_url for both models are:
def get_absolute_url(self):
return reverse(
"internalcontrol:objective_detail",
kwargs={"pk": self.pk},
)
and
def get_absolute_url(self):
return (
reverse("internalcontrol:task_detail", kwargs={"pk": self.pk}),
)
The traceback is as follows:
Internal Server Error: /task_create/
Traceback (most recent call last):
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\contrib\auth\mixins.py", line 73, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 184, in post
return super().post(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 153, in post
return self.form_valid(form)
File "C:\Users\User\Documents\django projects\nguyena\internalcontrol\views.py", line 231, in form_valid
return super().form_valid(form)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 136, in form_valid
return super().form_valid(form)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 65, in form_valid
return HttpResponseRedirect(self.get_success_url())
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\http\response.py", line 538, in __init__
self["Location"] = iri_to_uri(redirect_to)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\utils\encoding.py", line 139, in iri_to_uri
return quote(iri, safe="/#%[]=:;$&()+,!?*@'~")
File "c:\users\user\appdata\local\programs\python\python39\lib\urllib\parse.py", line 871, in quote
return quote_from_bytes(string, safe)
File "c:\users\user\appdata\local\programs\python\python39\lib\urllib\parse.py", line 896, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
[18/Apr/2022 17:08:13] "POST /task_create/ HTTP/1.1" 500 125026
Solution 1:[1]
I think the problem is the final address to which the user should be redirected.
To solve this problem, first edit the get_absolute_url function to return a string, not a tuple.
If it still does not work, try adding a success_url to the view with reverse_lazy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hamidreza Khorammfar |
