Django: redirecting to an external URL stored in the dB

Solution for Django: redirecting to an external URL stored in the dB
is Given Below:

I’m building a Django application. My end goal is for the user to be able to click a link to be redirected to an external site. Here’s my code:

models.py

class Entry(models.Model):
    manufacturer = models.CharField(max_length=255)
    source_url = models.URLField(blank=True)

views.py

def purchase(request, entry_id):
    entry = get_object_or_404(Entry, pk=entry_id)
    return redirect(entry.source_url)

entry.html

{% if user.is_authenticated %}
    <a href="{% url 'purchase' %}">Purchase!</a>
{% endif %}

urls.py

urlpatterns = [
    path('', views.index, name="index"),
    path('entries/<int:entry_id>', views.entry, name="entry"),
]

The data in the database looks as follows:

id    manufacturer     source_url
1     Mercedes         https://www.mbusa.com/en/home
2     BMW              https://www.bmw.com/en/index.html
3     Audi             https://www.audiusa.com/us/web/en.html

The error message I’m getting is:

Exception Value:    
Reverse for 'purchase' not found. 'purchase' is not a valid view function or pattern name.

As a test, I changed the following line of code:

From: <a href="{% url 'purchase' %}">Go to Source!</a>

To: <a href="www.google.com">Go to Source!</a>

This eliminated the “reverse” error, but the URL that it created was:

http://127.0.0.1:8000/entries/www.google.com

Is the “reverse” error caused by the lack of a URL route for “purchase” in urls.py? If yes, how would I define this?

Thanks in advance for helping this Django newbie!

For redirect from view you can do:

from django.http import HttpResponseRedirect
def purchase(request, entry_id):
    entry = get_object_or_404(Entry, pk=entry_id)
    return HttpResponseRedirect(entry.source_url)

and you can solve <a href="www.google.com">Go to Source!</a> problem by:

<a href="http://www.google.com">Go to Source!</a>

entry.html

{% if user.is_authenticated %}
    <a href="{% url 'purchase' entry_id %}">Purchase!</a>
{% endif %}

here entry_id will be the primary key of your Entry Table.

urls.py

urlpatterns = [
    path('', views.index, name="index"),
    path('entries/<int:entry_id>', views.entry, name="entry"),
    path('purchase/<int:entry_id>', views.purchase, name="purchase"),
]