Django Error: TemplateDoesNotExist at /trade/confirm/

Viewed 67

When a user clicks a button in a form, I am sending a POST request in a form with action="{% url 'trade-confirm' %}". The method that is called, insert_transaction, is working correctly. But when that method is finished it goes to confirmed-trade/<int:pk>/, which is showing the below error:

TemplateDoesNotExist at /trade/confirm/

confirmed-trade/20/

Screenshot of error here: enter image description here

Below is my code:

Folder structure: - Folder structure screenshot

game-exchange
-blog
--migrations/
--static/
--templates/
---blog/
----about.html
----base.html
----matches.html
----transaction.html
----your-trades.html
--admin.py
--apps.py
--models.py
--urls.py
--views.py
-django_project/
-media/
-users/
-manage.py
-posts.json
-Procfile

blog/urls.py

urlpatterns = [ #the 2 relevant url's
    path('confirmed-trade/<int:pk>/', views.TransactionDetailView.as_view(), name='confirmed-trade'), # Loads transaction form
    path('trade/confirm/', views.insert_transaction, name='trade-confirm'), # url to accept POST call from Your Trades page to create a Transaction
]

blog/models.py (transaction object only)

class Transaction(models.Model):
    name = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    trade_one = models.ForeignKey(Trade, on_delete=models.CASCADE, related_name='trade_one', db_column='trade_one')
    trade_two = models.ForeignKey(Trade, on_delete=models.CASCADE, related_name='trade_two', db_column='trade_two')
        status = models.TextField(default='Waiting for 2nd confirmation') 

    def get_transaction_name(self):
        return ''.join([self.trade_one_id, ' and ', self.trade_two_id, ' on ', timezone.now().strftime("%b %d, %Y %H:%M:%S UTC"), ')'])

    def save(self, *args, **kwargs):
        self.name = self.get_transaction_name()
        super(Transaction, self).save(*args, **kwargs)

    def __str__(self):
        return self.name 

    def get_absolute_url(self):
        return reverse('confirmed-trade', kwargs={'pk': self.pk})

blog/views.py:

def insert_transaction(request):
    if 'trade_1_id' not in request.POST or 'trade_2_id' not in request.POST:
        messages.add_message(request, DANGER, 'There was a problem confirming this trade. Please try again.')
        return redirect('/matches')

    trade_one_id = request.POST['trade_1_id']
    trade_two_id = request.POST['trade_2_id']

    transaction = Transaction(trade_one_id=trade_one_id, trade_two_id=trade_two_id)
    transaction.save()
    return render(request, 'confirmed-trade/' + str(transaction.id) + '/')

class TransactionDetailView(DetailView):
    model = Transaction

UPDATE: I did solve the issue for a short time, but the same error came back:

In addition to the answer below, I also had to update the TEMPLATES var in settings.py to be:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

This solved the issue for a short time, but I'm running into it again. The weird part is that if I manually go to the url http://localhost:8001/confirmed-trade/50/, it loads the template. But when the insert_transaction calls return render(request, 'confirmed-trade/' + str(transaction.id) + '/'), I get the error. Can someone please help me? I'm really stuck here

1 Answers

You need to specify template_name in TransactionDetailView like so:

class TransactionDetailView(DetailView):
    model = Transaction
    template_name = 'blog/transaction.html'

By default template_name is set to <app_label>/<model_name>_detail.html (blog/transaction_detail.html in your case)

EDIT:
Also, you need to pass a template name when calling render (this was causing the error), and I would consider using redirect:

from django.shortcuts import redirect  # Add import

def insert_transaction(request):
    # some logic ...

    transaction = Transaction(trade_one_id=trade_one_id, trade_two_id=trade_two_id)
    transaction.save()
    return redirect(transaction)  # or redirect('confirmed-trade', pk=transaction.pk)
Related