How to redirect Django button for loop in HTML to specific HTML page for that service?
Question:
I’m working on a django project but got stuck here.
I have a for loop which loops through the data from a model, basically looping in services which are offered and creating buttons for these.
I would like to redirect the button to a specific page for only this service. So basically each service offered gets an own button and if clicked gets redirected to a page for this service only.
As I add the link in an anchor I don’t know how to get it from the for loop in HTML doc.
How should I code this to get the desired result?
models.py
class ServiceTypes(models.Model):
typ = models.CharField(max_length=255, primary_key=True, blank=False)
pris = models.DecimalField(decimal_places=0, max_digits=6, null=False, blank=False)
beskrivning = models.CharField(max_length=255, null=True, blank=True)
views.py
def boka_service(request):
services = ServiceTypes.objects.all()
return render(request, 'main/boka.html', {'services': list(services)})
template file:
{% for r in services %}
<a href="{% url 'homepage' %}">
<button class="button_x">
<p style="margin-top: -60px; font-size: 15px; font-weight: bold;">{{ r.typ }} {{ r.pris }},- kr</p>
<p>{{ r.beskrivning }}</p>
</button></a>
{% endfor %}
Answers:
In your urls.py
add a path like:
from myapp.views import detail
urlpatterns = [
...
path('detail/<str:service_type>', detail, name='detail'),
...
]
Create a view which manages looking up the detail and rendering your detail template; something like the following in your views.py
:
from django.shortcuts import render, get_object_or_404
def detail(request, service_type=None):
service_type = get_object_or_404(ServiceTypes, typ=service_type)
context = {'service_type': service_type, }
return render(request, "your_file_that_shows_detail.html", context)
Then on your anchor href you can do something like:
{% for r in services %}
<a href="{% url 'detail' r.typ %}">
which should pass along that service type to the detail view, and render the page as you want.
I’m working on a django project but got stuck here.
I have a for loop which loops through the data from a model, basically looping in services which are offered and creating buttons for these.
I would like to redirect the button to a specific page for only this service. So basically each service offered gets an own button and if clicked gets redirected to a page for this service only.
As I add the link in an anchor I don’t know how to get it from the for loop in HTML doc.
How should I code this to get the desired result?
models.py
class ServiceTypes(models.Model):
typ = models.CharField(max_length=255, primary_key=True, blank=False)
pris = models.DecimalField(decimal_places=0, max_digits=6, null=False, blank=False)
beskrivning = models.CharField(max_length=255, null=True, blank=True)
views.py
def boka_service(request):
services = ServiceTypes.objects.all()
return render(request, 'main/boka.html', {'services': list(services)})
template file:
{% for r in services %}
<a href="{% url 'homepage' %}">
<button class="button_x">
<p style="margin-top: -60px; font-size: 15px; font-weight: bold;">{{ r.typ }} {{ r.pris }},- kr</p>
<p>{{ r.beskrivning }}</p>
</button></a>
{% endfor %}
In your urls.py
add a path like:
from myapp.views import detail
urlpatterns = [
...
path('detail/<str:service_type>', detail, name='detail'),
...
]
Create a view which manages looking up the detail and rendering your detail template; something like the following in your views.py
:
from django.shortcuts import render, get_object_or_404
def detail(request, service_type=None):
service_type = get_object_or_404(ServiceTypes, typ=service_type)
context = {'service_type': service_type, }
return render(request, "your_file_that_shows_detail.html", context)
Then on your anchor href you can do something like:
{% for r in services %}
<a href="{% url 'detail' r.typ %}">
which should pass along that service type to the detail view, and render the page as you want.