Cannot get is_valid() to go back to template with bound form

In the Django docs, it says

“We call the form’s is_valid() method; if it’s not True, we go back to
the template with the form. This time the form is no longer empty
(unbound) so the HTML form will be populated with the data previously
submitted, where it can be edited and corrected as required.”

I am trying to get this behaviour to work.


def test(request):
    if request.method == 'POST':
        form = TestForm(request.POST)

        if form.is_valid():
            return redirect('auctions/test.html')

        form = TestForm()
        return render(request, 'auctions/test.html', {'form': form})


class TestForm(forms.Form):
    testnumber = forms.DecimalField(max_digits=1)

In test.html:

<form action="{% url 'auctions:test' %}" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">

Now, when I submit the form with a number that is more than 1 digit, is_valid() evaluates to False. When that happens, the docs says the template should be rendered with the bound form and the error. Instead, nothing happens, and I get ValueError: The view auctions.views.test didn't return an HttpResponse object. It returned None instead.

Go to Source
Author: Ryan Eom

Trying to figure out running multiple queries with single endpoint based on the input parameters

I am trying to create a system, which has multiple queries for different kinds of reports. My plan is to queries in the database tag them report A, report B. Obvisouly SQL query will have some placeholders to pass paramterers.

I am trying reduce my coding work. So that i’ll keep adding queries in the database without touching the code.

i have explored diffrent django tool, but i haven’y found any suitable tool.

Any guidence will be apperictaed.

Go to Source
Author: wizard

REST API Testing best strategy

I’m currently developing a personal project using Django REST + React and as it grows more complex, I want to add unit testing to ensure that changes don’t affect previous functionality. However, I’m not sure exactly what to test for. Should I simply test the responses from the API endpoints, should I try random input to check if it’s validated properly, should I try getting access or changing data that belong to a different user? What is the best strategy to ensure that a REST API is both stable and secure through testing?

Go to Source
Author: Antonis Karvelas

How can I link an object on save in django to an existing one if there is a matching value between them?

I’m using Django and DRF to to reconcile two lists of data (promises to pay, and payments). When uploading new payments, if the payment transaction ID shares the same ID as a promise to pay, I want to link them by saving the corresponding promise in the payment’s foreign key field.

I have two models:

class Promise(models.Model):
     transaction_id = models.CharField(max_length=50)
     amount_promised = models.DecimalField(decimal_places=2, max_digits=8)
     created_on = models.DateTimeField(auto_now_add=True)

class Payment(models.Model):
    promise = models.OneToOneField(Promise, on_delete=models.CASCADE, blank=True)
    transaction_id = models.CharField(max_length=50)
    amount_paid = models.DecimalField(decimal_places=2, max_digits=8)
    created_on = models.DateTimeField(auto_now_add=True)

and two views:

class PromiseViewSet(viewsets.ModelViewSet):
    permission_classes = (IsCreatorOrReadOnly,)
    queryset = Promise.objects.all()
    serializer_class = PromiseSerializer

class PaymentViewSet(viewsets.ModelViewSet):
    permission_classes = (IsCreatorOrReadOnly,)
    queryset = Payment.objects.all()
    serializer_class = PaymentSerializer

My understanding is this requires a custom save method, but I’m not certain if the magic should be happening in views or models. Either way, the logic should be something like:

  • Loop through promises…if the new payment.transaction_id = a promise.transaction_id, break and link the two together by saving the to the promise foreign key in the newly created payment object.

And the solution would look something like this:

warning, this is rough!

def save(self, *args, **kwargs):
    #check if promise ID was manually entered, if not continue
    if self.promise is None:
        for transaction_id in Promise.transaction_id:

            if transaction_id = self.transaction_id:
                self.promise =  


    super(Payment, self).save(*args, **kwargs)

Go to Source
Author: Mike

Django: Where to instantiate a singleton?

In my django projects I have a series of external libraries that I use, such as the Stripe Python SDK and the Pusher Python SDK.

Is there a good way to instantiate these, so that I only use one instance everywhere? I was thinking I could instantiate it in, but it seems a bit weird.

Go to Source
Author: aroooo