Django: No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model in django?

after updating some fields in my model and hit the update button, i get this error that says No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.

I am redirecting to the dashboard after the update is done, but it seem not to be redirecting to that url anylonger, NOTE: the url still exists and works fine

what could the problem be here?

class ProductInline():
    form_class = PredictionForm
    model = Predictions
    template_name = "core/create/create_bet.html"

    def form_valid(self, form):
        named_formsets = self.get_named_formsets()
        if not all((x.is_valid() for x in named_formsets.values())):
            return self.render_to_response(self.get_context_data(form=form))

        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.current_amount = self.object.amount
        self.object.save()

        for name, formset in named_formsets.items():
            formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None)
            if formset_save_func is not None:
                formset_save_func(formset)
            else:
                formset.save()
            messages.success(self.request, "Your bet have been created.")
        return redirect('core:dashboard')

    def formset_variants_valid(self, formset):
        """
        Hook for custom formset saving.. useful if you have multiple formsets
        """
        variants = formset.save(commit=False)  # self.save_formset(formset, contact)
        # add this, if you have can_delete=True parameter set in inlineformset_factory func
        for obj in formset.deleted_objects:
            obj.delete()

        for variant in variants:
            variant.predictions = self.object
            variant.save()



class PredictionUpdate(ProductInline, UpdateView):

    def get_context_data(self, **kwargs):
        ctx = super(PredictionUpdate, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx
    
    def get_current_object(self, id):
        prediction = Predictions.objects.get(id=id)
        return {
            "prediction":prediction
        }

    def get_named_formsets(self):
        return {
            'variants': PredictionDataFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object, prefix='variants'),
        }

    def dispatch(self, request ,*args, **kwargs):
        obj = self.get_object()
        if obj.user != self.request.user:
            messages.warning(self.request, "You are not allowed to edit this bet!")
            return redirect("core:dashboard")
        
        if obj.status == "finished":
            messages.warning(self.request, "You cannot edit this bet again, send an update request to continue.")
            return redirect("core:dashboard")
            # raise Http404("You are not allowed to edit this Post")
        return super(PredictionUpdate, self).dispatch(request, *args, **kwargs)
    
    def form_valid(self, form):
        instance = form.instance
        if instance.participants.exists() and instance.status == 'cancelled':
            messages.warning(self.request, "You cannot cancel a bet that already have participants")
            return redirect("core:dashboard")
        else:
            form.save()
            return HttpResponseRedirect(self.get_success_url())

You’re calling get_success_url. That function (by default) returns self.success_url.

I don’t see where you override either get_success_url or success_url to provide a value.

i have overridden the get_success_url()

def get_success_url(self):
        return redirect("core:dashboard")

I am getting this new error now

TypeError at /update/34/
quote_from_bytes() expected bytes

what could be going wrong here?

It would be helpful to see the complete traceback to be able to identify the line that is causing this.

This is the full traceback

System check identified no issues (0 silenced).
December 26, 2022 - 15:34:34
Django version 3.2.7, using settings 'bet_prj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /update/34/
Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner    
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Destiny\Desktop\Bettings App Versions\Version 3 - Peer to Peer - Prediction have overall amount and each bet object thier 
odds\bet_prj\core\create_bet.py", line 98, in dispatch
    return super(PredictionUpdate, self).dispatch(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\mixins.py", line 71, in dispatch     
    return super().dispatch(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 98, in dispatch      
    return handler(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 194, in post
    return super().post(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Users\Destiny\Desktop\Bettings App Versions\Version 3 - Peer to Peer - Prediction have overall amount and each bet object thier 
odds\bet_prj\core\create_bet.py", line 110, in form_valid
    return HttpResponseRedirect(self.get_success_url())
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\http\response.py", line 501, in __init__
    self['Location'] = iri_to_uri(redirect_to)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\encoding.py", line 147, in iri_to_uri       
    return quote(iri, safe="/#%[]=:;$&()+,!?*@'~")
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\urllib\parse.py", line 853, in quote
    return quote_from_bytes(string, safe)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\urllib\parse.py", line 878, in quote_from_bytes
    raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
[26/Dec/2022 15:34:35] "POST /update/34/ HTTP/1.1" 500 121658

What data type is get_success_url supposed to return?

get_success_url returns Strings.

Correct. What is your get_success_url returning? (See redirect)

it returning a redirect(...) method.

After looking at the redirect i changed my return to this this return "/dashboard"

Not quite - it is returning the value that the redirect function returns. So you would want to look at the doc for redirect to identify what the data type is that it returns.

The function you would actually want to use in that situation would be reverse.

I am using reverse now and it seems to be working, is this how it is supposed to be?

def get_success_url(self):
        return reverse("core:dashboard")

If it’s doing what you want it to do and not throwing any errors, then yes.