0
votes

My view currently returns an error of

The view viajecenter.views.receive_payment didn't return an HttpResponse object. It returned None instead.

I tried some of the solutions from other related posts here but of no luck.

Here is my function in my views.py file:

def receive_payment(request, account_code):
template_name = 'receive-payment.html'

user = request.user

account = get_object_or_404(Account, account_code=account_code)
if user.is_assigned:
    puv = Puv.objects.get(assignment_id=user.assignment_id)
    locations = puv.route.locations.all().order_by('distance_from_base')

    context = {
        'account': account,
        'puv': puv,
        'locations': locations,
    }
    return render(request, template_name, context)

else:
    return user

and the corresponding url in urls.py file:

from django.urls import path
from . views import ..., receive_payment


urlpatterns = [
    ...
    path('receive-payment/<str:account_code>', receive_payment, name="receive-payment"),
]

and my Account model:

class Account(AbstractBaseUser, PermissionsMixin):
    ...
    account_code = models.UUIDField(default=uuid.uuid4, editable=False)
    is_assigned = models.BooleanField(default=False)

Thank you for lending me your time and answering my question :)

1
Your code makes no sense. get_context_data is only a thing for class-based views, not function-based views like your receive_payment currently is.AKX
Plus I think I explained this to you in your previous question?Abdul Aziz Barkat
My bad, sorry. @AbdulAzizBarkat did corrected my format in my other post. I have edited my post and hopefully it makes a bit more sense :)Allen

1 Answers

2
votes

It seems you're trying to mix and match function-based views and class-based views.

You can rewrite your view as a DetailView class-based view like so:

from django.views.generic import DetailView


class ReceivePaymentView(DetailView):
    template_name = "receive-payment.html"
    model = Account
    slug_url_kwarg = "account_code"
    slug_field = "account_code"

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        account = context["object"]
        user = self.request.user
        if user.is_assigned:
            puv = Puv.objects.get(assignment_id=user.assignment_id)
            locations = puv.route.locations.all().order_by("distance_from_base")
            context["account"] = account
            context["puv"] = puv
            context["locations"] = locations
        return context
urlpatterns = [
    path('receive-payment/<account_code>', ReceivePaymentView.as_view(), name="receive-payment"),
]